Python & unicode
michele.simionato at gmail.com
michele.simionato at gmail.com
Tue Jan 11 04:15:00 EST 2005
I forgot to add the following:
>>> setattr(C, "è", u"The letter è")
>>> getattr(C, "è")
u'The letter \xe8'
>>> print getattr(C, "è")
The letter è
Python identifiers can be generic strings, including Latin-1
characters;
they cannot be unicode strings, however:
>>> setattr(C, u"è", "The letter è")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in
position 0: ordinal not in range(128)
So you are right after all, but I though most people didn't know that
you can have
valid identifiers with accented letters, spaces, and non printable
chars.
> setattr(C, " ", "this works")
> getattr(C, " ")
Michele Simionato
More information about the Python-list
mailing list