Dictless classes

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jul 3 07:03:36 CEST 2012

You can create instances without a __dict__ by setting __slots__:

py> class Dictless:
...     __slots__ = ['a', 'b', 'c']
py> Dictless().__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Dictless' object has no attribute '__dict__'

But the class itself still has a __dict__:

py> Dictless.__dict__
dict_proxy({'a': <member 'a' of 'Dictless' objects>, 'c': <member 'c' of 
'Dictless' objects>, 'b': <member 'b' of 'Dictless' objects>, 
'__module__': '__main__', '__slots__': ['a', 'b', 'c'], '__doc__': None})

I wonder whether there is some metaclass magic one can do to create a 
class without a __dict__?

I don't have a use-case for this. But I have some code which assumes that 
every class will have a __dict__, and I wonder whether that is a safe 


More information about the Python-list mailing list