1

I quite often use the combination of pprint and the 'dict' attribute while writing new python code and every now and again I hit the error:

AttributeError: 'dict' object has no attribute '__dict__'

I know what this means, but I can't help wondering why it wasn't added as a convenience just referencing itself.

If we look further at the dict object, it would seem it has been deliberately left out.

a = {}
dir(a)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']

I'm sure it was a decision not to include this, I'd like to know why.

joeButler
  • 1,643
  • 1
  • 20
  • 41
  • 3
    None of the standard types have a `__dict__` (in CPython at least - see e.g. `dir([])`, `dir('')`). – jonrsharpe Jan 29 '16 at 09:12
  • Yeah this is true looking a bit deeper, I guess its just that the times I'm using this I'm often looking at class objects containing data. I guess a better question would have been how I can avoid hitting this problem – joeButler Jan 29 '16 at 09:46

0 Answers0