为什么 class 变量可以通过 class 实例访问?
Why is class variable accessible via class instance?
我创建了没有实例变量的 class Circle
。我添加了一个 class 方法 from_diameter
来生成给定直径的圆。
class Circle:
@classmethod
def from_diameter(cls, diameter):
cls.diameter = diameter
return cls
diameter
是一个 class(静态)变量。但是 - 看起来它作为实例变量和 class 变量存在。
myCircle = Circle.from_diameter(10)
print Circle.diameter
print myCircle.diameter
输出:
10
10
为什么有效?没有实例变量diameter
。
我想 print myCircle.diameter
应该会抛出一个错误。
没有实例变量,只是 Python 对象中的名称查找首先查看实例,然后,如果未找到匹配项,则进入 class.
Class instances
A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. [...] If no class attribute is found, and the object’s class has a __getattr__()
method, that is called to satisfy the lookup.
当您尝试使用 class 访问变量时,它只会查看
cls.__dict__
但是当您尝试使用实例访问变量时,它首先查找
self.__dict__
如果找到则 return 或者如果找不到则它也在
中查找
cls.__dict__
这里的 cls 是 class
class Test:
temp_1=10
temp_2=20
def __init__(self):
self.test_1=10
self.test_2=20
@classmethod
def c_test(cls):
pass
def t_method(self):
pass
print Test.__dict__
print Test().__dict__
输出:
{'c_test': <classmethod object at 0x7fede8f35a60>, '__module__': '__main__', 't_method': <function t_method at 0x7fede8f336e0>, 'temp_1': 10, '__doc__': None, '__init__': <function __init__ at 0x7fede8f335f0>, 'temp_2': 20}
{'test_2': 20, 'test_1': 10}
我创建了没有实例变量的 class Circle
。我添加了一个 class 方法 from_diameter
来生成给定直径的圆。
class Circle:
@classmethod
def from_diameter(cls, diameter):
cls.diameter = diameter
return cls
diameter
是一个 class(静态)变量。但是 - 看起来它作为实例变量和 class 变量存在。
myCircle = Circle.from_diameter(10)
print Circle.diameter
print myCircle.diameter
输出:
10
10
为什么有效?没有实例变量diameter
。
我想 print myCircle.diameter
应该会抛出一个错误。
没有实例变量,只是 Python 对象中的名称查找首先查看实例,然后,如果未找到匹配项,则进入 class.
Class instances
A class instance is created by calling a class object (see above). A class instance has a namespace implemented as a dictionary which is the first place in which attribute references are searched. When an attribute is not found there, and the instance’s class has an attribute by that name, the search continues with the class attributes. [...] If no class attribute is found, and the object’s class has a
__getattr__()
method, that is called to satisfy the lookup.
当您尝试使用 class 访问变量时,它只会查看
cls.__dict__
但是当您尝试使用实例访问变量时,它首先查找
self.__dict__
如果找到则 return 或者如果找不到则它也在
中查找cls.__dict__
这里的 cls 是 class
class Test:
temp_1=10
temp_2=20
def __init__(self):
self.test_1=10
self.test_2=20
@classmethod
def c_test(cls):
pass
def t_method(self):
pass
print Test.__dict__
print Test().__dict__
输出:
{'c_test': <classmethod object at 0x7fede8f35a60>, '__module__': '__main__', 't_method': <function t_method at 0x7fede8f336e0>, 'temp_1': 10, '__doc__': None, '__init__': <function __init__ at 0x7fede8f335f0>, 'temp_2': 20}
{'test_2': 20, 'test_1': 10}