How to Suppress Interactive Assignment to "_"

Discussion in 'Python' started by JKPeck, Jan 1, 2010.

  1. JKPeck

    JKPeck Guest

    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
     
    JKPeck, Jan 1, 2010
    #1
    1. Advertising

  2. JKPeck

    Peter Otten Guest

    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
     
    Peter Otten, Jan 1, 2010
    #2
    1. Advertising

  3. JKPeck

    JKPeck Guest

    On Jan 1, 10:06 am, Peter Otten <> 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
     
    JKPeck, Jan 1, 2010
    #3
  4. En Fri, 01 Jan 2010 15:53:31 -0300, JKPeck <> escribió:
    > On Jan 1, 10:06 am, Peter Otten <> 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
     
    Gabriel Genellina, Jan 4, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. peshrad
    Replies:
    3
    Views:
    1,511
    David Freeman
    Nov 8, 2004
  2. =?Utf-8?B?SmFzb24gUGV0dHlz?=

    Suppress viewstate __viewstate rendering

    =?Utf-8?B?SmFzb24gUGV0dHlz?=, Jan 14, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    2,920
    Steven Cheng[MSFT]
    Jan 18, 2005
  3. WJ
    Replies:
    2
    Views:
    501
  4. nagy
    Replies:
    36
    Views:
    1,015
    Terry Reedy
    Jul 20, 2006
  5. Chris
    Replies:
    34
    Views:
    1,535
Loading...

Share This Page