How to Suppress Interactive Assignment to "_"

JKPeck jkpeck at gmail.com
Fri Jan 1 19:53:31 CET 2010


On Jan 1, 10:06 am, Peter Otten <__pete... at 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.
>
> > 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.
>
> $ 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

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



More information about the Python-list mailing list