Access to variable from external imported module

robert no-spam at no-spam-no-spam.invalid
Fri Nov 24 10:23:54 CET 2006


GinTon wrote:
> Thanks Robert, the best solution is get all local variables, else is
> impossible access to them.

For test purposes/ex post inspection you could also uncomment the line in:

def f(a=1):
    b=2
    c=3
    #globals().update(locals())
    return a+b
--

then it is more easy and you can get it like:

module.c


You can also create a total stack trace dynamically with this trick function:

def mktb():
    try: raise UserWarning
    except: return sys.exc_info()[2]

def f(a=1):
    b=2
    c=3
    global ftb;ftb=mktb()
    return a+b
----

and then fully inspect the total situation in the func (and all down the call history) ex post at any time with

>>> f()
>>> pdb.post_mortem(module.ftb)               # then do once 'up' in pdb/pywin.debugger...
>>> pywin.debugger.post_mortem(module.ftb)


Which other programming language can do things like this? 
( Unfortunately (legacy) Python has no possibility to (re-)continue execution from exceptions/traces other than by simple generators )

Robert



> robert ha escrito:
>> GinTon wrote:
>>> I would to access to values that are created locally in that method
>> after the method has executed? usually the return value?
>> or you want to get all local variables, then make a func/method
>>
>> def f(a=1):
>>     b=2
>>     c=3
>>     return locals()  #X/Object(locals())
>>
>> --------
>>
>> d=module.f()
>> print d['c']         # d.c
> 



More information about the Python-list mailing list