instance.__dict__ & class.__dict__

后端 未结 2 1063
北海茫月
北海茫月 2021-02-10 23:33

Code goes first:

class A(object):
    def foo(self):
        self.foo = \'foo\'
    def bar(self):
        self.bar = \'bar\'

cls_dict = dict(A.__dict__)  #not          


        
2条回答
  •  [愿得一人]
    2021-02-11 00:29

    a.__dict__ is empty because none of the attributes have been initialized on the instance as there's no __init__() defined. Meanwhile, a class is an object, and it has initialized some attributes.

    >>> A
    
    

提交回复
热议问题