user196264097
user196264097

Reputation: 887

What is the dfifference between instance dict and class dict

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

Upvotes: 8

Views: 12402

Answers (6)

bereal
bereal

Reputation: 34282

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.

Upvotes: 2

Burhan Khalid
Burhan Khalid

Reputation: 174624

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__']

Upvotes: 12

chase
chase

Reputation: 3782

Rohit Jain has the simplest python code to explain this quickly. However, understanding the same ideas in Java can be useful, and there is much more information about class and instance variables here

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213223

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.

Upvotes: 6

sureshvv
sureshvv

Reputation: 4412

You can define attributes separately on a per instance basis rather than for the whole class

For eg.

class A(object):
    an_attr = 0

a1 = A()
a2 = A()

a1.another_attr = 1

Now a2 will not have another_attr. That is part of the instance dict rather than the class dict.

Upvotes: 0

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

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

Upvotes: 1

Related Questions