interning strings
Peter Otten
__peter__ at web.de
Mon Nov 8 03:51:38 EST 2004
"Martin v. Löwis" wrote:
> Peter Otten wrote:
>> String constants that are potential attribute names are also interned:
>
> Peter has explained all this correctly, but this aspect needs some
> stressing perhaps: string *literals* that are potential attribute
> names are also interned. This interning is done in the compiler,
> when the code object is created, so strings not created by the compiler
> are not interned.
>
> [all strings are "constant", i.e. immutable, so the statement
> above might have been confusing]
Yes, string "literal", not "constant" is the appropriate term for what I
meant.
For completeness here is an example demonstrating that names appearing as
"bare words" in the code are interned:
>>> class X:
... def __getattr__(self, name):
... return name
...
>>> a = X().this_is_an_attribute
>>> X().this_is_an_attribute is a
True
Peter
More information about the Python-list
mailing list