469,908 Members | 2,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,908 developers. It's quick & easy.

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 internationalization. Now, this function is overwritten
in interactive mode, which causes annoying exceptions. Is it possible to
disable this behavior?
Jul 1 '08 #1
3 1256
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 internationalization. Now, this function is overwritten
in interactive mode, which causes annoying exceptions. Is it possible to
disable this behavior?
>>import sys
def displayhook(result):
.... if result is not None:
.... __builtins__._last = result
.... print result
....
>>"yadda"
yadda
>>_
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '_' is not defined
>>_last
yadda

Peter
Jul 1 '08 #2
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 internationalization. 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(result):
.... if result is not None:
.... __builtins__._last = result
.... print result
....
>>sys.displayhook = 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
Jul 1 '08 #3
Peter Otten <__*******@web.dewrites:
>import sys
def displayhook(result):
... if result is not None:
... __builtins__._last = 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(result):
if result is not None:
__builtin__._last = result
print result

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Roy Yao | last post: by
7 posts views Thread by Scott W Gifford | last post: by
4 posts views Thread by =?Utf-8?B?SmFu?= | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.