88
class C(object):
    def f(self):
        print self.__dict__
        print dir(self)
c = C()
c.f()

output:

{}

['__class__', '__delattr__','f',....]

why there is not a 'f' in self.__dict__

kunigami
  • 3,046
  • 4
  • 26
  • 42
shellfly
  • 892
  • 2
  • 13
  • 20

3 Answers3

155

dir() does much more than look up __dict__

First of all, dir() is a API method that knows how to use attributes like __dict__ to look up attributes of an object.

Not all objects have a __dict__ attribute though. For example, if you were to add a __slots__ attribute to your custom class, instances of that class won't have a __dict__ attribute, yet dir() can still list the available attributes on those instances:

>>> class Foo(object):
...     __slots__ = ('bar',)
...     bar = 'spam'
... 
>>> Foo().__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__dict__'
>>> dir(Foo())
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', 'bar']

The same applies to many built-in types; lists do not have a __dict__ attribute, but you can still list all the attributes using dir():

>>> [].__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute '__dict__'
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

What dir() does with instances

Python instances have their own __dict__, but so does their class:

>>> class Foo(object):
...     bar = 'spam'
... 
>>> Foo().__dict__
{}
>>> Foo.__dict__.items()
[('__dict__', <attribute '__dict__' of 'Foo' objects>), ('__weakref__', <attribute '__weakref__' of 'Foo' objects>), ('__module__', '__main__'), ('bar', 'spam'), ('__doc__', None)]

The dir() method uses both these __dict__ attributes, and the one on object to create a complete list of available attributes on the instance, the class, and on all ancestors of the class.

When you set attributes on a class, instances see these too:

>>> f = Foo()
>>> f.ham
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute 'ham'
>>> Foo.ham = 'eggs'
>>> f.ham
'eggs'

because the attribute is added to the class __dict__:

>>> Foo.__dict__['ham']
'eggs'
>>> f.__dict__
{}

Note how the instance __dict__ is left empty. Attribute lookup on Python objects follows the hierarchy of objects from instance to type to parent classes to search for attributes.

Only when you set attributes directly on the instance, will you see the attribute reflected in the __dict__ of the instance, while the class __dict__ is left unchanged:

>>> f.stack = 'overflow'
>>> f.__dict__
{'stack': 'overflow'}
>>> 'stack' in Foo.__dict__
False

TLDR; or the summary

dir() doesn't just look up an object's __dict__ (which sometimes doesn't even exist), it will use the object's heritage (its class or type, and any superclasses, or parents, of that class or type) to give you a complete picture of all available attributes.

An instance __dict__ is just the 'local' set of attributes on that instance, and does not contain every attribute available on the instance. Instead, you need to look at the class and the class's inheritance tree too.

mikefrey
  • 4,653
  • 3
  • 29
  • 40
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 4
    `dir` will also look at `__dir__` if implemented, which can return strings for things that aren't even implemented as formal properties or attributes, but are simply lazily-implemented in `__getattr__`, for instance. – PaulMcG Jan 16 '13 at 15:55
  • 1
    @PaulMcGuire: I didn't want to go into too much detail as to what `dir()` does; it's a long answer as it is, and I didn't need to include that detail to get the point across. :-) – Martijn Pieters Jan 16 '13 at 15:59
  • See [what's the difference between `dir(self)` and `self.__dict__`](http://stackoverflow.com/questions/13302917/whats-the-difference-between-dirself-and-self-dict/13302981#13302981) for a more concise answer. – Peterino Feb 26 '17 at 16:42
  • When I use dir on an object that has `@Property` getter / setter decorators, it returns that "public" name of the property (I realise these leading underscores are just naming conventions and not real access modifiers) . When I use `__dict__` it returns only the private attributes e.g. this DatasourceItem object has properties like `id` which `dir` will return, but `__dict__` returns `_id` https://github.com/tableau/server-client-python/blob/master/tableauserverclient/models/datasource_item.py – Davos Mar 27 '18 at 12:28
  • 2
    @Davos: A `property` object is a descriptor object that lives on the class, not on the instance. What a property getter or setter or deleter does is *entirely implementation dependent*. A property can do anything it pleases, it doesn't have to do anything with the instance at all. `dir()` on an instance will still list any attributes on the class *and* any attributes in the instance `__dict__`, so `dir()` on a `DatasourceItem` instance will show both the `connections` property defined on the class, and the `_connections` attribute on the instance. – Martijn Pieters Mar 27 '18 at 17:57
  • 1
    @Davos: so yes, that's entirely expected. The `DatasourceItem.id` `property` object lives on the class, not on the instance. `instance.id` will trigger the getter method, which then returns `self._id`, where `self._id` is an attribute on the instance. – Martijn Pieters Mar 27 '18 at 17:58
  • Thanks for the explanation, that does clear things up for me. I was attempting to retrieve an existing datasource object, use most of its attributes in a new object instance and then pass that back to the server to overwrite. I thought perhaps fetching the attributes as a dictionary would be a bit more dynamic, but just fetching the attribute values with dot notation is probably better in terms of "coding to the interface", and there's only a few non-defaults. I was being lazy and trying to automate something that needn't be. – Davos Mar 28 '18 at 13:06
6

The function f belongs to the dictionary of class C. c.__dict__ yields attributes specific to the instance c.

>>> class C(object):
    def f(self):
        print self.__dict__


>>> c = C()
>>> c.__dict__
{}
>>> c.a = 1
>>> c.__dict__
{'a': 1}

C.__dict__ would yield attributes of class C, including function f.

>>> C.__dict__
dict_proxy({'__dict__': <attribute '__dict__' of 'C' objects>, '__module__': '__main__', '__weakref__': <attribute '__weakref__' of 'C' objects>, '__doc__': None, 'f': <function f at 0x0313C1F0>})

While an object can refer to an attribute of its class (and indeed all the ancestor classes), the class attribute so referred does not become part of the associated dict itself. Thus while it is legitimate access to function f defined in class C as c.f(), it does not appear as an attribute of c in c.__dict__.

>>> c.a = 1
>>> c.__dict__
{'a': 1}
Naveen Kumar
  • 61
  • 1
  • 2
4

Class dir() and __dict__ only show defined functions (methods). Instance dir() shows data+methods while its __dict__ only shows data attributes, not methods.

class C2:
...     def __init__(self, id): 
...         self.id = id
...     def set(self, len):
...         self.len = len        
...         
c2 = C2("stick")
dir(C2)
['__class__',.., '__init__', 'set']
dir(c2)
['__class__',.., '__init__', 'id', 'set']
C2.__dict__
mappingproxy({'__init__': <function C2.__init__ at 0x000001899C61E9D0>, 'set': <function C2.set at 0x000001899C61ED30>,...})
c2.__dict__
{'id': 'stick'}
tony
  • 870
  • 7
  • 16
Leon Chang
  • 669
  • 8
  • 12