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

samwyse samwyse at gmail.com
Thu Dec 6 15:35:04 EST 2007


On Nov 24, 6:38 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> samwyse <samw... 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".

D'oh!  That's what I get for not memorizing "Non-essential Built-in
Functions".

In my defense, however, my function will work with anything that can
be used as a dictionary key (strings, tuples, frozen sets, etc), not
just character strings; thus we return to the original:

>>> a=(1,2,3)
>>> b=(1,1+1,1+1+1)
>>> a == b
True
>>> a is b
False
>>> atom(a) is atom(b)
True
>>> intern(a) is intern(b)
Traceback (most recent call last):
  File "<pyshell#33>", line 1, in ?
    intern(a) is intern(b)
TypeError: intern() argument 1 must be string, not tuple



More information about the Python-list mailing list