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
Matrix24
V2EX  ›  Python

Python 类内方法函数的内部变量,命名,加'self.'变成 self.xxx 和不加直接 xxx 有什么区别?

  •  
  •   Matrix24 · 2014-03-02 18:37:57 +08:00 · 7015 次点击
    这是一个创建于 3707 天前的主题,其中的信息可能已经有所发展或是发生改变。
    具体的如下:
    class AAA(object):
    def Go(self):
    self.one = 'hello'

    和下面的
    class AAA(object):
    def Go(self):
    one = 'hello'

    两者的one,有什么区别?
    15 条回复    1970-01-01 08:00:00 +08:00
    Matrix24
        1
    Matrix24  
    OP
       2014-03-02 18:50:01 +08:00
    为毛不可以修改问题。问题是这样子的:
    具体的如下:
    class AAA(object):
    ····def Go(self):
    ········self.one = 'hello'

    和下面的
    class AAA(object):
    ····def Go(self):
    ········one = 'hello'

    两者的one,有什么区别?
    binux
        2
    binux  
       2014-03-02 18:53:26 +08:00
    我去,你自己发了两篇讲这个的文章你自己都没看过吗?
    Matrix24
        3
    Matrix24  
    OP
       2014-03-02 18:55:39 +08:00
    @binux 哈哈,文章还是有瑕疵。除了这个地方没说。你知道么,可爱的网友?
    binux
        4
    binux  
       2014-03-02 18:56:50 +08:00
    hahastudio
        5
    hahastudio  
       2014-03-02 19:01:07 +08:00   ❤️ 1
    https://gist.github.com/hahastudio/9304929

    你自己试试就知道了
    AAA中的one,是attribute
    下面的AAA中的one,是method Go的local variable
    Matrix24
        6
    Matrix24  
    OP
       2014-03-02 19:06:35 +08:00
    @binux 其实我就想问一句,class下的def XXX里面的定义的变量,加self和不加有什么区别,它是不是和其他的一样?

    因为我现在认为__init__()下是一个特殊的区域,在这里定义self.xxx是正常可以引用的实例化变量
    但是在class下和def并列的时候,定义 self.XX就不会引用成功
    同样,我怀疑在def内,变量加和不加self都不同。

    我有点混乱,求指点
    binux
        7
    binux  
       2014-03-02 19:12:17 +08:00
    @Matrix24 __init__除了一开始被自动调用以外,没有任何特殊的地方
    Matrix24
        8
    Matrix24  
    OP
       2014-03-02 19:19:37 +08:00
    @binux
    class AAA():
    ········self.a='5'
    ········def go(self):
    ················pass


    b=AAA()
    b.a

    ---------
    为什么不可以在class内和def平行关系,定义self.XXX这种实例化变量?
    iEverX
        9
    iEverX  
       2014-03-02 19:22:15 +08:00
    class Example(object):
    a = 0 # 定义在类里面,是类的属性

    def hello(self):
    self.c = 3 #

    def print(self):
    print self.a, self.c


    e = Example()
    e.hello()
    e.print() # 0 3
    binux
        10
    binux  
       2014-03-02 19:23:06 +08:00
    @Matrix24 哪来的self?你以为你写def go(self):的self都是摆看的啊?
    Matrix24
        11
    Matrix24  
    OP
       2014-03-02 19:28:06 +08:00
    @binux 你的意思是说,所有定义的self.XXX是函数内的(self)决定的,突然明白了。哈哈,被打醒了,ThankYou,谢谢你的耐心~~
    wuyadong
        12
    wuyadong  
       2014-03-02 22:30:17 +08:00
    ....
    Fedor
        13
    Fedor  
       2014-03-02 22:36:41 +08:00
    一个是对象的属性。
    一个是方法里的局部变量。
    qianlifeng
        14
    qianlifeng  
       2014-03-03 09:15:04 +08:00
    self的一个功能就是为了区分对象属性和局部变量?
    Shared
        15
    Shared  
       2014-03-03 12:23:10 +08:00   ❤️ 1
    @qianlifeng 不是为了区分,而是 self 就是 class 实例化后的对象本身,你把 self 改成别的都无所谓,只不过大家约定的用 self
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3181 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:33 · PVG 22:33 · LAX 07:33 · JFK 10:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.