intern'ed strings and deepcopy()

Peter Hansen peter at engcorp.com
Fri Apr 11 22:25:44 EDT 2003


Alexander Schmolck wrote:
> 
> Erik Max Francis <max at alcyone.com> writes:
> 
> > My point was that if you find yourself wanting to make a truly unique
> > copy of a string (an immutable object), then there is almost certainly
> > something wrong with your design.
> 
> Hmm, do you mean if you want ``x is not y`` but ``x == y`` for interned string
> x and some string y?
> 
> But I'm not sure that that's what the OP's up to. E.g. you could have a
> container mapping symbols to other things -- possibly also symbols or further
> containers. You then might want to make a deepcopy of it. You presumably still
> want the symbols to be the same symbols in the copy (so that you can make use
> of the identity), so asking whether ``deepcopy(symbol) is symbol`` doesn't
> seem that unreasonable to me (is it?).

What is this "symbol" that you keep talking about?  If you
are simply using that as an alias for "interned string", then
I believe you are making up reasons for those which they are
*not* intended to have.  You said "the idea of symbols is their
uniqueness".  Well, I don't know what a "symbol" is to you,
but I believe that "interned strings" are *not* intended to 
guarantee some "uniqueness" property which you may count on 
in your code.  Any such property is a side effect of the
implementation or something.  I believe the sole purpose of
an interned string ("symbol"?) is to *optimize* access to those
strings, i.e. for performance reasons.  

See http://www.python.org/doc/current/lib/built-in-funcs.html#built-in-funcs
under "intern()" for what I believe is the only official purpose
of interning strings: "to gain a little performance on dictionary lookup".

-Peter




More information about the Python-list mailing list