V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
leisurelylicht
V2EX  ›  Python

类内的变量可以作为类方法的参数的默认值吗?

  •  
  •   leisurelylicht · 2015-09-10 22:39:12 +08:00 · 2120 次点击
    这是一个创建于 3160 天前的主题,其中的信息可能已经有所发展或是发生改变。
    class W ():
        def __init__(self ):
            super (W, self ).__init__()
            self.url = 'http://api'
    
        def dataRequest (self,url=self.url ):
    
        test = W ()
        test.dataRequest ()
    

    向上面这样,调用 dataRequest ()不带参数时就用 self.url,带参数时就用参数.

    已经试过这么写无法执行的,就想问下是没有这种使用方法还是我的方法写错了?

    10 条回复    2015-09-11 22:03:46 +08:00
    publicID002
        1
    publicID002  
       2015-09-10 22:59:17 +08:00
    def dataRequest (self, url=None ):
    url = url or self.url
    print (url )
    leisurelylicht
        2
    leisurelylicht  
    OP
       2015-09-10 23:04:14 +08:00
    @publicID002 只能在方法内做判断吗
    soyxulu
        3
    soyxulu  
       2015-09-10 23:11:35 +08:00 via iPhone
    看下那个位置的 self 是什么呢 到底能不能获取到 self.url
    def dataRequest (self,obj = self ):
    print obj.url
    leisurelylicht
        4
    leisurelylicht  
    OP
       2015-09-10 23:19:26 +08:00
    @soyxulu 应该是不可以,显示 NameError: name 'self' is not defined
    hahasong
        5
    hahasong  
       2015-09-10 23:24:36 +08:00   ❤️ 1
    默认参数只能是常量,不关 self 写在哪里什么事
    hahasong
        6
    hahasong  
       2015-09-10 23:34:36 +08:00
    @hahasong https://docs.python.org/2/tutorial/controlflow.html#default-argument-values
    The default values are evaluated at the point of function definition in the defining scope
    leisurelylicht
        7
    leisurelylicht  
    OP
       2015-09-11 00:07:10 +08:00
    @hahasong 但是这种方法就可以啊
    i = 5

    def f (arg=i ):
    print arg

    f (7 )
    deanguqiang
        8
    deanguqiang  
       2015-09-11 00:22:37 +08:00
    类的方法都是非资料描述器,在其 __get__() 方法里面会把 obj.fun (*args ) 调用自动改成 fun (obj, *args ) ,所以第一个参数 self 会被传入对象实例,但是其余参数是不知道 self 是什么东西的。
    leisurelylicht
        9
    leisurelylicht  
    OP
       2015-09-11 15:09:55 +08:00
    @deanguqiang 请问有什么相关资料吗,有点看不懂
    deanguqiang
        10
    deanguqiang  
       2015-09-11 22:03:46 +08:00
    @leisurelylicht
    你可以搜索一下描述器 descriptor 。或者看看这个链接: http://pyzh.readthedocs.org/en/latest/Descriptor-HOW-TO-Guide.html#id6

    关键是知道 python 中 self 并不是一个关键词,那么对象是怎么传递给方法呢?就是方法的时候第一个形参(通常是 self ,也可以改成其他的)会被传入对象,其余的参数是原封不动给函数的。这里实现的机制是描述器,即如果类里面定义的方法是:

    def fun (self, arg=self.bar ):
    pass

    调用方法的时候 obj.fun () 被改成 fun (obj, arg=self.bar ),这个时候 self 是未定义的,所以会出错。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1012 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:14 · PVG 02:14 · LAX 11:14 · JFK 14:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.