__dict__ attribute for built-in types

Chris Angelico rosuav at gmail.com
Thu Oct 27 09:25:19 EDT 2011


On Thu, Oct 27, 2011 at 10:03 PM, Duncan Booth
<duncan.booth at invalid.invalid> wrote:
> Types without a __dict__ use less memory. Also, if you couldn't have a
> type that didn't have a `__dict__` then any `dict` would also need its
> own `__dict__` which would either result in infinite memory use or
> recursive dictionaries.
>

Easy, just self-reference.
a = {}
a.__dict__ is a  --> True

Yeah, it's recursion, but no different from types:

>>> type(type) is type
True

If you want this behavior, you can do it easily enough.

>>> class dictdict(dict):
	def __init__(self):
		self.__dict__=self

>>> a=dictdict()
>>> a.__dict__ is a
True

However, the more compelling argument is that a __slots__ object can
be WAY more efficient.

ChrisA



More information about the Python-list mailing list