Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python difference in __dict__ between object() and class myClass(object)

Tags:

python

I was messing around with dynamic attributes and I noticed I could not use the __dict__ attribute if I created the object directly from the object() class but if I create a new class that is a direct descendent of object I can access the __dict__ attribute. Why the difference?

Examples:

# This gives an AttributeError  
o = object()
o.__dict__
# This works: prints {}
class myClass(object):
    pass
o = myClass()
o.__dict__
like image 593
Jamie Czuy Avatar asked May 10 '11 18:05

Jamie Czuy


1 Answers

object is implemented in C and doesn't have a __dict__ attribute. (Not all Python objects have it either; look up __slots__).

like image 89
kindall Avatar answered Oct 27 '22 16:10

kindall