How can I create customized classes that have similar properties as 'str'?

Hrvoje Niksic hniksic at xemacs.org
Sat Nov 24 20:48:30 EST 2007


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> On Sun, 25 Nov 2007 01:38:51 +0100, Hrvoje Niksic wrote:
>
>> samwyse <samwyse at gmail.com> writes:
>> 
>>> create a hash that maps your keys to themselves, then use the values of
>>> that hash as your keys.
>> 
>> The "atom" function you describe already exists under the name "intern".
>
> Not really. intern() works very differently, because it can tie itself to 
> the Python internals.

The exact implementation mechanism is subtly different, but
functionally intern is equivalent to the "atom" function.

> In any case, I'm not sure that intern() actually will solve the OP's
> problem, even assuming it is a real and not imaginary
> problem. According to the docs, intern()'s purpose is to speed up
> dictionary lookups, not to save memory. I suspect that if it does
> save memory, it will be by accident.

It's not by accident, it follows from what interning does.  Interning
speeds up comparisons by returning the same string object for the same
string contents.  If the strings you're working with tend to repeat,
interning will save some memory simply by preventing storage of
multiple copies of the same string.  Whether the savings would make
any difference for the OP is another question.

> From the docs: 
> http://docs.python.org/lib/non-essential-built-in-funcs.html
>
> intern(  string)
> Enter string in the table of ``interned'' strings and return the interned 
> string - which is string itself or a copy. [...]
>
> Note the words "which is string itself or a copy". It would be ironic if 
> the OP uses intern to avoid having copies of strings, and ends up with 
> even more copies than if he didn't bother.

That's a frequently misunderstood sentence.  It doesn't mean that
intern will make copies; it simply means that the string you get back
from intern can be either the string you passed it or another
(previously interned) string object that is guaranteed to have the
same contents as your string (which makes it technically a "copy" of
the string you passed to intern).



More information about the Python-list mailing list