编程语言
首页 > 编程语言> > python – 用self调用类变量

python – 用self调用类变量

作者:互联网

你怎么想出这个有趣的(至少对我而言)的例子.

import numpy as np


class Something(object):
    a = np.random.randint(low=0, high=10)

    def do(self):
        self.a += 1
        print(self.a)

if __name__ == '__main__':
    something = Something()
    print(something.__str__())
    something.do()
    something2 = Something()
    print(something2.__str__())
    something2.do()
    something3 = Something()
    print(something3.__str__())
    something3.do()

以上内容在控制台中打印以下内容:

$python test.py
<__main__.Something object at 0x7f03a80e0518>
1
<__main__.Something object at 0x7f03a80cfcc0>
1
<__main__.Something object at 0x7f03a80cfcf8>
1

我有点困惑,因为我(错误地)认为a的值会增加.

如果我使用@classmethod装饰器,我能够获得我期望的行为.

import numpy as np


class Something(object):
    a = np.random.randint(low=0, high=10)

    @classmethod
    def do(cls):
        cls.a += 1
        print(cls.a)

if __name__ == '__main__':
    something = Something()
    print(something.__str__())
    something.do()
    something2 = Something()
    print(something2.__str__())
    something2.do()
    something3 = Something()
    print(something3.__str__())
    something3.do()

这会在控制台中正确打印以下内容.

python test.py
<__main__.Something object at 0x7faac77becc0>
3
<__main__.Something object at 0x7faac77becf8>
4
<__main__.Something object at 0x7faac77c3978>
5

现在,我想知道第一个例子,当我打电话给self.a时,我正在访问什么?它不是一个类变量,因为我似乎无法改变它的值.它也不是一个实例变量,因为它似乎在同一个类的不同对象之间共享.你怎么称呼它?

这是一个我正以错误方式使用的类变量吗?我知道cls名称是一个约定,所以也许我真正访问一个类变量,但我无法更改其值,因为我没有使用@classmethod装饰器装饰该方法.

这是一种非法使用的语言吗?我的意思是,最好的做法是不要做以避免在后期引入错误?

解决方法:

发生的事情是self.a在不同的时间指的是两件事.

如果名称不存在实例变量,Python将查找该类的值.因此,为self.a检索的值将是类变量.

但是当通过self设置属性时,Python将始终设置实例变量.所以现在self.a是一个新的实例变量,其值等于类变量1.该属性隐藏了class属性,你不能再通过self访问它,只能通过类访问.

(一个小问题,与问题无关:你永远不应该直接访问双下划线方法.而不是调用something2 .__ str __(),调用str(something2)等)

标签:python,python-3-x,instance-variables,class-variables
来源: https://codeday.me/bug/20191007/1863537.html