S
Steven D'Aprano
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
assumption.
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
assumption.