您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python中的“ __class__”属性到底是什么

python中的“ __class__”属性到底是什么

“从以上代码中,我认为NewCounter1.count等于NewCounter1。class@H_403_3@.count”

问题是,在您的问题中此句子出现时,仅在以下说明之后:

NewCounter1 = counter()
NewCounter2 = counter()
NewCounter2.__class__.count = 3
@H_404_10@

创建了 和 并修改了类属性 , 不存在对象 和 ,因此 “等于”@H_403_3@ 没有实际意义。

请在之后查看 的创建:

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
@H_404_10@

是实例的名称空间 print NewCounter1.count@H_404_10@ 打印一样print counter.count@H_404_10@ 。然而,“计数”(字符串“数”)是不是在命名空间 ,也就是说没有属性 在命名空间创建的实例!

这怎么可能 ?

这是因为没有assignement到“计数”标识内部创建实例的 - >有任何属性作为一个字段没有真正的创造 ,也就是说没有创建实例属性

结果是,在print 'NewCounter1.count ==',NewCounter1.count@H_404_10@ 评估指令时,解释器不会在 的名称空间中找到实例属性,然后转到该实例的类以在此类的名称空间中搜索键“ count”。在那里,它找到“ count”作为CLASS属性的键,并且可以将对象 的VALUE作为要响应该指令显示的VALUE。

类实例具有一个实现为字典的名称空间,这是搜索属性引用的第一位。当在那里找不到属性,并且实例的类具有该名称属性时,将继续使用类属性进行搜索http://docs.python.org/reference/datamodel.html#the-standard-type- hierarchy

因此,NewCounter1.count equals NewCounter1.__class__.count@H_404_10@这意味着NewCounter1.count的VALUE即使该名确实不存在,也是类属性的VALUE 。这里的“是”是英语动词,不是该功能 测试两个对象的身份的语言,它的意思是“被认为具有”

NewCounter2.__class__.count = 3@H_404_10@被执行时,只有类属性 受到影响。 和 的命名空间保持为空,并 相同的机制来访问类以找到 的值。

最后,当NewCounter2.count = 5@H_404_10@执行时,这一次将在INSTANCE属性 中创建一个 对象中的字段,并且“ count”出现在 的命名空间中 。 它不会覆盖任何内容,因为实例中的内容之前__dict__@H_404_10@ 没有任何内容。其他更改不会影响 和

以下代码更明确地显示了执行期间的基础事件:

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)
@H_404_10@

结果

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
@H_404_10@

有趣的事情是self.count = counter.count@H_404_10@ 在该行之前添加一条指令self.__class__.count += 1 # <<=====@H_404_10@ 以观察结果的变化

综上所述,重点并不在乎,__class__@H_404_10@而是一种搜索属性的机制,而这种机制在被忽略时会产生误导。

python 2022/1/1 18:25:31 有167人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶