Printing literal text of an argument
Reinhold Birkenfeld
reinhold-birkenfeld-nospam at wolke7.net
Sat Aug 13 05:28:50 EDT 2005
Rex Eastbourne wrote:
> Thanks. I adapted it a bit:
>
> def debug(foo):
> print foo, 'is:'
> exec('pprint.pprint(' + foo + ')')
>
> But I'm getting "NameError: name 'foo' is not defined," since foo is
> not defined in this scope. (The function works beautifully when I'm
> dealing with global variables, which is very rarely).
>
> Any way around this?
Eh, yes, but it's getting uglier...
def debug(foo):
print foo, 'is:'
frame = sys._getframe(-1)
exec 'pprint.pprint(%s)' % foo, frame.f_globals, frame.f_locals
Untested.
Reinhold
More information about the Python-list
mailing list