Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Suppress Interactive Assignment to "_"

0 views
Skip to first unread message

JKPeck

unread,
Jan 1, 2010, 10:41:30 AM1/1/10
to
The gettext module uses the convention of defining a function named
"_" that maps text into its translation.
This conflicts with the automatic interactive interpreter assignment
of expressions to a variable with that same name.

While if you are careful, you can avoid that assignment while
debugging, and you can choose a different function for gettext, this
conflict is a nuisance. I am looking for a way to suppress the
expression assignment to _ or to change the name of the variable
assigned to. Is this possible? Using Python 2.6.

TIA,
Jon Peck

Peter Otten

unread,
Jan 1, 2010, 12:06:18 PM1/1/10
to
JKPeck wrote:


$ cat displayhook.py
import sys
import __builtin__ as builtin

def displayhook(obj):
if obj is not None:
builtin._last_result = obj
print repr(obj)

sys.displayhook = displayhook
$ python -i displayhook.py
>>> 42
42
>>> _
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
>>> _last_result
42

JKPeck

unread,
Jan 1, 2010, 1:53:31 PM1/1/10
to

Thanks. It's just what I needed.
-Jon Peck

Gabriel Genellina

unread,
Jan 4, 2010, 3:51:06 AM1/4/10
to pytho...@python.org
En Fri, 01 Jan 2010 15:53:31 -0300, JKPeck <jkp...@gmail.com> escribiᅵ:

> On Jan 1, 10:06 am, Peter Otten <__pete...@web.de> wrote:
>> JKPeck wrote:

>> > The gettext module uses the convention of defining a function named
>> > "_" that maps text into its translation.
>> > This conflicts with the automatic interactive interpreter assignment
>> > of expressions to a variable with that same name.
>>

>> $ cat displayhook.py
>> [...]


>
> Thanks. It's just what I needed.

In case you didn't know, look at sitecustomize.py and the PYTHONSTARTUP
[2] environment variable. They allow for the above code to be
automatically executed.

[1] http://docs.python.org/library/site.html#index-549
[2] http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP

--
Gabriel Genellina

0 new messages