Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the dfifference between instance dict and class dict

Tags:

python

I was reading the python descriptors and there was one line there

Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary.

I am really confused what is instance dict and what is class dictionary

Can anyone please explain me with code what is that

I was thinking of them as same

like image 957
user196264097 Avatar asked Feb 10 '13 07:02

user196264097


4 Answers

An instance dict holds a reference to all objects and values assigned to the instance, and the class level dict holds all references at the class namespace.

Take the following example:

>>> class A(object):
...    def foo(self, bar):
...       self.zoo = bar
...
>>> i = A()
>>> i.__dict__ # instance dict is empty
{}
>>> i.foo('hello') # assign a value to an instance
>>> i.__dict__ 
{'zoo': 'hello'} # this is the instance level dict
>>> i.z = {'another':'dict'}
>>> i.__dict__
{'z': {'another': 'dict'}, 'zoo': 'hello'} # all at instance level
>>> A.__dict__.keys() # at the CLASS level, only holds items in the class's namespace
['__dict__', '__module__', 'foo', '__weakref__', '__doc__']
like image 66
Burhan Khalid Avatar answered Oct 19 '22 13:10

Burhan Khalid


I think, you can understand with this example.

class Demo(object):
    class_dict = {}   # Class dict, common for all instances

    def __init__(self, d):
        self.instance_dict = d   # Instance dict, different for each instance

And it's always possible to add instance attribute on the fly like this: -

demo = Demo({1: "demo"})
demo.new_dict = {}  # A new instance dictionary defined just for this instance

demo2 = Demo({2: "demo2"})   # This instance only has one instance dictionary defined in `init` method

So, in the above example, demo instance has now 2 instance dictionary - one added outside the class, and one that is added to each instance in __init__ method. Whereas, demo2 instance has just 1 instance dictionary, the one added in __init__ method.

Apart from that, both the instances have a common dictionary - the class dictionary.

like image 31
Rohit Jain Avatar answered Oct 19 '22 13:10

Rohit Jain


Those dicts are the internal way of representing the object or class-wide namespaces.

Suppose we have a class:

class C(object):
    def f(self):
        print "Hello!"

c = C()

At this point, f is a method defined in the class dict (f in C.__dict__, and C.f is an unbound method in terms of Python 2.7).

c.f() will make the following steps:

  • look for f in c.__dict__ and fail
  • look for f in C.__dict__ and succeed
  • call C.f(c)

Now, let's do a trick:

def f_french():
    print "Bonjour!"

c.f = f_french

We've just modified the object's own dict. That means, c.f() will now print Bounjour!. This does not affect the original class behaviour, so that other C's instances will still speak English.

like image 39
bereal Avatar answered Oct 19 '22 11:10

bereal


Class dict is shared among all the instances (objects) of the class, while each instance (object) has its own separate copy of instance dict.

like image 32
Mikhail Vladimirov Avatar answered Oct 19 '22 11:10

Mikhail Vladimirov