instance.__dict__ & class.__dict__

后端 未结 2 1071
北海茫月
北海茫月 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:17

    In Python, everything is an object containing attributes. That includes classes themselves. The whole object-oriented thing with classes which define methods and instances which can execute those methods with the varying data per-instance is just a protocol for looking up attributes in a series of objects.

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

    This creates a class object, bound to the name A. That object contains two attributes, foo and bar, each bound to method objects.

    a = A()
    

    This creates an object bound to the name a, which is an instance of A. It contains no attributes at all, yet. But the fact that it is an instance of A means the "lookup protocol" when you ask for a.foo is to first look for an attribute on a, then if that fails look for an attribute on A. This is how all instances of A can access the same set of methods.

    In Python, also, you can create attributes on (almost) any object at any time. All it requires is some_object.some_attribute = some_value. Until you do that, the attribute does not exist. So with actually calling a.foo() or a.bar(), those foo and bar attributes do not exist in a (neither do any other attributes your program may or may not one day add to that object), which is why a.__dict__ is empty at the point where you called it. If you expect 'default' values of foo and bar to exist on every object, you need to say so by setting those values in the __init__ method.

提交回复
热议问题