By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,421 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,421 IT Pros & Developers. It's quick & easy.

Disabling the magic "last expression" _ variable

P: n/a
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
Share this Question
Share on Google+
3 Replies

P: n/a
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

P: n/a
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

P: n/a
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.