How to use _ in interactive mode

pythonic pythonic at gmail.com
Fri Nov 25 11:31:43 CET 2005


Hi,

I use '_' for localization in my program. The problem is when testing
the program using python intractive mode I lose _ function.
One solution is put following in PYTHONSTARTUP file.
--
import readline
def __enforce_underscore__ (): __builtins__._ = str
readline.set_pre_input_hook (__enforce_underscore__)
--

My program in startup imports a module which customize the env.
Putting above in that module doesn't solve the problem.  Any
cleaner/better way?
Honestly, I was expecting once overriden _ in builtins it would be honored.

This how I reproduce the problem.

Python 2.3.3 (#1, May  7 2004, 10:31:40)
>>> def localizer(s): return str(s)
>>> __builtins__.__dict__['_'] = localizer
>>> _
<function localizer at 0xb7f6e844>
>>> _('some text')
'some text'
>>> _
'some text'
>>> _('some text')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable

TIA.



More information about the Python-list mailing list