Creating a List of Empty Lists

Emile van Sebille emile at fenx.com
Sat Dec 6 13:52:01 EST 2003


Robin Munn:
> But take note that this behavior is not guaranteed anywhere in the
language
> reference. As someone else said in this thread, don't count on
interning.
> Sometimes it will happen, and sometimes it won't:
>
> >>> a = 'gobble'
> >>> a is gobble
> True
> >>> b = 'foo bar'
> >>> b is 'foo bar'
> False
>
> The rule of thumb is that Python interns a string if it's likely to
be used
> as a name (i.e., only alphanumerics and underscores). The string
'foo bar'
> has a space and would be an invalid name, so it wasn't interned.
>

And don't think you can get around it using intern():

Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on
win32
>>> b = intern('foo bar')
>>> a = 'foo bar'
>>> a is 'foo bar'
False
>>> b is 'foo bar'
False

That apparent space requirement should really be better documented.

>>> b = intern('foo_bar')
>>> b is 'foo_bar'
True
>>> a = 'foo_bar'
>>> a is 'foo_bar'
True

>From the docs on intern():
  "Interning strings is useful to gain a little performance on
dictionary lookup "

and while it continues:
   "...names used in Python programs are automatically interned, and
the dictionaries used to hold module, class or instance attributes
have interned keys"

It probably should specifically state that only valid identifiers will
be intern()'d and optionally return an error or warning otherwise.



Emile van Sebille
emile at fenx.com





More information about the Python-list mailing list