Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to Suppress Interactive Assignment to "_"

Reply
Thread Tools

How to Suppress Interactive Assignment to "_"

 
 
JKPeck
Guest
Posts: n/a
 
      01-01-2010
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-01-2010
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

 
Reply With Quote
 
 
 
 
JKPeck
Guest
Posts: n/a
 
      01-01-2010
On Jan 1, 10:06*am, Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      01-04-2010
En Fri, 01 Jan 2010 15:53:31 -0300, JKPeck <(E-Mail Removed)> escribió:
> On Jan 1, 10:06 am, Peter Otten <(E-Mail Removed)> 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...-PYTHONSTARTUP

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do you disable or suppress the '?' interactive help function John Chajecki Cisco 2 12-17-2005 03:32 PM
Interactive/Non-interactive ASPX ? WJ ASP .Net 2 02-26-2005 02:54 AM
non Interactive and Interactive AAH Computer Support 0 01-09-2005 04:09 PM
Interactive and non interactive AAH Computer Support 1 01-09-2005 04:01 PM



Advertisments