[Tutor] empty delimiters, and None

Steven D'Aprano steve at pearwood.info
Fri Nov 29 23:44:35 CET 2013


On Fri, Nov 29, 2013 at 08:19:37AM -0500, ugajin at talktalk.net wrote:

[...]
> After much continued searching I then found this 
> Bug #803791 report which describes (as an aside) the exact same issue 
> together with a solution.

It might help if you provide a link to that bug report. Remember, you've 
read it, but we haven't.


> I know next to nothing about Python and I shall be glad if any Python 
> guru can make clear (in layman's terms where possible) what the 
> problem is/was and how the fix works.
> 
> I remembered an alternative posting that I had read, which reported 
> that Python dislikes empty delimiters. 

In general, that is certainly not true. Empty delimiters like [], '', {} 
and even () are legal and perfectly accepted in Python in many places. 
More context would be needed.


[...]
> On trying to run the script (before the fix) I had the following traceback message: 
> File "measure.py", line 35, in <module>
> locale.setlocale(locale.LC_ALL, '')
> File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/locale.py", line 494, in setlocale
> return _setlocale(category, locale)
> locale.Error: unsupported locale setting

Ah, now we're getting somewhere!

The locale settings control the internationalization of your operating 
system. For example, instead of everything being English all throughout 
the world, using locale, your computer can display buttons and menus in 
Greek when in Greece, in Japanese in Japan, and so on, provided the 
correct settings are available.

The locale.setlocale() function takes two arguments, the first 
locale.LC_ALL is okay, the problem is with the second one. Originally it 
was set to the empty string '' which tells Python to return the current 
locale. Python complains about that which means either:

- your system has no locale set at all;

- your system is set to use a locale which has been removed from 
  your computer.

To fix this, you can set an environment variable. I don't know how to do 
this in OS-X, but in Linux systems I would edit my .bashrc config file 
and add the line:

export LANG="blah blah blah"

where "blah blah blah" is the name of a locale. You can find out which 
locales are available by running the system command:

locale -a 

at the shell. If you need help doing this, you can try asking here, with 
luck somebody will know how to do this on a Mac.

For reference, here is the relevant part of the docs:

http://docs.python.org/2/library/locale.html#locale.setlocale


> If I booted from the Unix file I received the following feedback:
> Setting Language: .UTF-8
> (process:82224): Gtk-WARNING **: Locale not supported by C library.
Using the fallback 'C' locale.

I have no idea what you mean by this, sorry. What does "booted from the 
Unix file" mean?


> Line 35 (above) shows the empty delimiter. The fix that I applied was 
> to replace line 35 in the Measure.py script with: 
> locale.setlocale(locale.LC_ALL, None). The Measure Path script then 
> executes correctly. No other action is/was required.

I'm glad that this fixes the problem for you, but I'd be wary about 
leaving None in place. Passing None rather than '' does subtly different 
things, and I'm not sure if the Measure Path script does what it is 
supposed to do with None in place.

Still, it appears to work, and I guess that provided there are no 
obvious problems with it, it seems harmless enough.

The best thing would be to report this problem, and the apparent fix, to 
the author of the Measure Path script, and see what he or she thinks 
about it.


> I note also the Python 2 library entry for Built-in Constants as:
> None
> The sole value of types.NoneType. 
[...]

Thanks for that, but we're very familiar with None, as Python 
programmers you can barely go five minutes without needing to use None 
or see None in a piece of code.


-- 
Steven


More information about the Tutor mailing list