Re: [Tutor] IDLE vs. Python Command Line

Magnus Lycka magnus at thinkware.se
Thu Jun 24 16:35:39 EDT 2004


Orri wrote:
> when i type:
> >>>import string
> >>>string.letters
> in IDLE, i get:
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\x83\x8a\x8c\x8e\x9a\x9c\
> x9e\x9f\xaa\xb5\xba\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xc
> e\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\
> xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf
> 6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
> but when i do it on the Python Command Line, i get:
> 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
> 
> is this a flaw in IDLE or the Command Line? 

Maybe... I agree that it's a bit confusing. I'm not sure why they default
to different locales.

> Which is more accurate? 

Both are accurate, but in different contexts. :)

> Since one of my programs uses string.letters, it is important for me to know.

As I'm sure you know, the set of letters is different in different
contexts. If you ask someone in Japan, they are likely to say that
both sequences fail to describe the letters.

If you run this...

>>> import locale
>>> locale.getlocale()

I assume you will get different results in IDLE and at the command line. At 
least I do in Windows. I also get strings that are constent with the locale 
settings. (Well, fairly at least, my GUI locale says 'Swedish_Sweden', '1252'
and I get all the letters in codepage 1252 as letters, but that's much more
than the Swedish letters of course...but that's how it is...)

I'm not sure why IDLE is set up to give a different default that the CLI 
version, but remember the Python mottos:

Explicit is better than implicit.
In the face of ambiguity, refuse the temptation to guess.

If you depend on string.letters to return only US ASCII letters, run this:

import locale
locale.setlocale(locale.LC_CTYPE, "C")

Another option is to use string.ascii_letters instead, which is always the same.

If you want the locale setting defined by the OS, use this:

import locale
locale.setlocale(locale.LC_CTYPE, "")

If you always set locale in your program, you won't depend on any unclear
defaults.

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list