Disabling the magic "last expression" _ variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Koen Vossen

    Disabling the magic "last expression" _ variable

    A project I'm currently working on uses Turbogears as a framework. I'd
    like to interact with it using the tg-admin shell. We also use gettext's
    _() function for internationaliz ation. Now, this function is overwritten
    in interactive mode, which causes annoying exceptions. Is it possible to
    disable this behavior?
  • Peter Otten

    #2
    Re: Disabling the magic "last expression&quot ; _ variable

    Koen Vossen wrote:
    A project I'm currently working on uses Turbogears as a framework. I'd
    like to interact with it using the tg-admin shell. We also use gettext's
    _() function for internationaliz ation. Now, this function is overwritten
    in interactive mode, which causes annoying exceptions. Is it possible to
    disable this behavior?
    >>import sys
    >>def displayhook(res ult):
    .... if result is not None:
    .... __builtins__._l ast = result
    .... print result
    ....
    >>"yadda"
    yadda
    >>_
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name '_' is not defined
    >>_last
    yadda

    Peter

    Comment

    • Peter Otten

      #3
      Re: Disabling the magic &quot;last expression&quot ; _ variable

      Koen Vossen wrote:
      A project I'm currently working on uses Turbogears as a framework. I'd
      like to interact with it using the tg-admin shell. We also use gettext's
      _() function for internationaliz ation. Now, this function is overwritten
      in interactive mode, which causes annoying exceptions. Is it possible to
      disable this behavior?
      [correct overzealous snip]
      >>import sys
      >>def displayhook(res ult):
      .... if result is not None:
      .... __builtins__._l ast = result
      .... print result
      ....
      >>sys.displayho ok = displayhook
      >>_
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      NameError: name '_' is not defined
      >>"yadda"
      yadda
      >>_
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      NameError: name '_' is not defined
      >>_last
      yadda


      Peter

      Comment

      • Ben Finney

        #4
        Re: Disabling the magic &quot;last expression&quot ; _ variable

        Peter Otten <__peter__@web. dewrites:
        >import sys
        >def displayhook(res ult):
        ... if result is not None:
        ... __builtins__._l ast = result
        ... print result
        ...
        Better is to explicitly import the name '__builtin__'
        <URL:http://www.python.org/doc/lib/module-builtin>. The name
        '__builtins__' is an implementation detail not guaranteed to be
        present in any particular implementation.

        import __builtin__
        import sys
        def displayhook(res ult):
        if result is not None:
        __builtin__._la st = result
        print result

        --
        \ “If you continue running Windows, your system may become |
        `\ unstable.” —Microsoft, Windows 95 error message |
        _o__) |
        Ben Finney

        Comment

        Working...