Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instance.__dict__ & class.__dict__

Code goes first:

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

cls_dict = dict(A.__dict__)  #not empty

a = A()
a_dict = dict(a.__dict__)  #empty, why?
like image 461
Alcott Avatar asked Jan 19 '23 13:01

Alcott


1 Answers

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.

like image 72
Ben Avatar answered Feb 01 '23 19:02

Ben