Interning own classes like strings for speed and size?

Hrvoje Niksic hniksic at xemacs.org
Tue Dec 28 15:16:51 EST 2010


Christian Heimes <lists at cheimes.de> writes:

> Also this code is going to use much more memory than an ordinary tuple
> since every instance of InternedTuple has a __dict__ attribute. This
> code works but I had to move the cache outside the class because of
> __slots__.

Wouldn't it work inside the class as well?  __slots__ applies to
instances, not to classes themselves.  In Python 3.1:

>>> class Foo(tuple):
...   __slots__ = ()
...   _cache = {}
... 
>>> Foo._cache    # works as expected
{}
>>> Foo()._cache  # and so does this
{}



More information about the Python-list mailing list