我对python中的__class__
有疑问。
该文档说__class__
是一个类实例所属的类。因此,我进行了一系列实验:
class counter:
count = 0
def __init__(self):
self.__class__.count += 1
NewCounter1 = counter()
print NewCounter1.count #The result is 1
NewCounter2 = counter()
print NewCounter2.count #The result is 2
print NewCounter2.__class__.count is NewCounter2.count #result: True
一切顺利。
然后我输入如下代码:
NewCounter2.__class__.count = 3
print NewCounter1.count #result:3
print NewCounter1.__class__.count #result:3
print NewCounter2.count #result:3
print NewCounter2.__class__.count #result:3
print NewCounter2.__class__.count is NewCounter2.count #result: True
从上面的代码中,我认为
NewCounter1.count
等于NewCounter1
或__class__.count
,但是以下代码使我感到惊讶:NewCounter2.count = 5
print NewCounter1.count #result:3
print NewCounter1.__class__.count #result:3
print NewCounter2.count #result:5
print NewCounter2.__class__.count #result:3
print NewCounter2.__class__.count is NewCounter2.count #result: False
为什么
NewCounter2.count
更改了,但NewCounter2.__class__.count
仍保持为3?而且,当我更改NewCounter2.count
时,NewCounter2.__class__.count is NewCounter2.count
变为False
。 __class__
属性到底是什么? 最佳答案
问题是,在您的问题中此句子出现时,仅在以下说明之后:
NewCounter1 = counter()
NewCounter2 = counter()
NewCounter2.__class__.count = 3
创建了 NewCounter1 和 NewCounter2
并修改了类属性 counter.count ,
不存在 NewCounter1.count 或 NewCounter2.count 的对象,因此“等于”没有实际意义。
。
请在之后查看 NewCounter1 的创建:
class counter:
count = 0
def __init__(self):
self.__class__.count += 1
print 'counter.count BEFORE ==',counter.count # The result is 0
NewCounter1 = counter()
print '\nNewCounter1.__dict__ ==',NewCounter1.__dict__ # The result is {}
print 'NewCounter1.count ==',NewCounter1.count # The result is 1
print 'counter.count AFTER ==',counter.count # The result is 1
NewCounter._dict_ 是实例的 namespace NewCounter1
print NewCounter1.count
与print counter.count
打印相同但是,“count”(字符串“count”)不在 NewCounter1 的命名空间中,也就是说,在创建的实例的命名空间中没有 count 属性!
这怎么可能 ?
这是因为创建实例时未分配 _init_ 内部的“count”标识符
->在 NewCounter1 中没有真正创建任何属性作为字段,也就是说,没有创建INSTANCE属性。
结果是当指令
print 'NewCounter1.count ==',NewCounter1.count
经过评估,解释器在 NewCounter1 的 namespace 中找不到实例属性,然后转到该实例的类以在此类的 namespace 中搜索键“count”;在那里,它找到“count”作为CLASS属性的键,并且可以将对象 counter的VALUE.count 作为要响应该指令显示的VALUE。因此,这里的
NewCounter1.count equals NewCounter1.__class__.count
表示NewCounter1.count的VALUE(即使该名称实际上并不存在)也是类属性 NewCounter1的VALUE。 class.count 。这里的“is”是英语动词,不是功能是测试两个对象身份的语言的,它的意思是“被认为具有”执行
NewCounter2.__class__.count = 3
时,仅影响类属性 counter.count 。 NewCounter1 和 NewCounter2 的命名空间保持为空,并使用相同的机制转到类以查找 counter.count 的值。。
最后,当执行
NewCounter2.count = 5
时,这次将在 NewCounter2 对象中的字段中创建一个INSTANCE属性 count ,并且'count'出现在 NewCounter2 的命名空间中。它不会覆盖任何内容,因为实例的
__dict__
中没有任何前缀没有其他更改影响 NewCounter1 和 counter.count
以下代码更明确地显示了执行期间的基础事件:
from itertools import islice
class counter:
count = 0
def __init__(self):
print (' | counter.count first == %d at %d\n'
' | self.count first == %d at %d')\
% (counter.count,id(counter.count),
self.count,id(self.count))
self.__class__.count += 1 # <<=====
print (' | counter.count second == %d at %d\n'
' | self.count second == %d at %d\n'
' | id(counter) == %d id(self) == %d')\
% (counter.count,id(counter.count),
self.count,id(self.count),
id(counter),id(self))
def display(*li):
it = iter(li)
for ch in it:
nn = (len(ch)-len(ch.lstrip('\n')))*'\n'
x = it.next()
print '%s == %s %s' % (ch,x,'' if '__dict__' in ch else 'at '+str(id(x)))
display('counter.count AT START',counter.count)
print ('\n\n----- C1 = counter() ------------------------')
C1 = counter()
display('C1.__dict__',C1.__dict__,
'C1.count ',C1.count,
'\ncounter.count ',counter.count)
print ('\n\n----- C2 = counter() ------------------------')
C2 = counter()
print (' -------------------------------------------')
display('C1.__dict__',C1.__dict__,
'C2.__dict__',C2.__dict__,
'C1.count ',C1.count,
'C2.count ',C2.count,
'C1.__class__.count',C1.__class__.count,
'C2.__class__.count',C2.__class__.count,
'\ncounter.count ',counter.count)
print '\n\n------- C2.__class__.count = 3 ------------------------\n'
C2.__class__.count = 3
display('C1.__dict__',C1.__dict__,
'C2.__dict__',C2.__dict__,
'C1.count ',C1.count,
'C2.count ',C2.count,
'C1.__class__.count',C1.__class__.count,
'C2.__class__.count',C2.__class__.count,
'\ncounter.count ',counter.count)
print '\n\n------- C2.count = 5 ------------------------\n'
C2.count = 5
display('C1.__dict__',C1.__dict__,
'C2.__dict__',C2.__dict__,
'C1.count ',C1.count,
'C2.count ',C2.count,
'C1.__class__.count',C1.__class__.count,
'C2.__class__.count',C2.__class__.count,
'\ncounter.count ',counter.count)
结果
counter.count AT START == 0 at 10021628
----- C1 = counter() ------------------------
| counter.count first == 0 at 10021628
| self.count first == 0 at 10021628
| counter.count second == 1 at 10021616
| self.count second == 1 at 10021616
| id(counter) == 11211248 id(self) == 18735712
C1.__dict__ == {}
C1.count == 1 at 10021616
counter.count == 1 at 10021616
----- C2 = counter() ------------------------
| counter.count first == 1 at 10021616
| self.count first == 1 at 10021616
| counter.count second == 2 at 10021604
| self.count second == 2 at 10021604
| id(counter) == 11211248 id(self) == 18736032
-------------------------------------------
C1.__dict__ == {}
C2.__dict__ == {}
C1.count == 2 at 10021604
C2.count == 2 at 10021604
C1.__class__.count == 2 at 10021604
C2.__class__.count == 2 at 10021604
counter.count == 2 at 10021604
------- C2.__class__.count = 3 ------------------------
C1.__dict__ == {}
C2.__dict__ == {}
C1.count == 3 at 10021592
C2.count == 3 at 10021592
C1.__class__.count == 3 at 10021592
C2.__class__.count == 3 at 10021592
counter.count == 3 at 10021592
------- C2.count = 5 ------------------------
C1.__dict__ == {}
C2.__dict__ == {'count': 5}
C1.count == 3 at 10021592
C2.count == 5 at 10021568
C1.__class__.count == 3 at 10021592
C2.__class__.count == 3 at 10021592
counter.count == 3 at 10021592
。
一个有趣的事情是添加一条指令
self.count = counter.count
上线之前self.__class__.count += 1 # <<=====
观察结果的变化。
总之,重点不在于
__class__
,而是关于属性搜索的机制,而该机制在被忽略时会产生误导。