passing global data to a function
Hung Jung Lu
hungjunglu at yahoo.com
Wed Dec 3 15:59:11 EST 2003
beliavsky at aol.com wrote in message news:<3064b51d.0312010855.66f4e2 at posting.google.com>...
> How can I pass global data to function stored in a separate file?
> For example, in file "funk.py" is the code
>
> ipow = 2
> def xpow(xx): return xx**ipow
>
> and in "xfunk.py" is the code
>
> from funk import xpow,ipow
> xx = 4.0
> print xpow(xx)
> ipow = 3
> print xpow(xx)
I am a bit tired of the standard answer, so let us try a funkier
approach! :)
# funk.py
ipow = 2
def xpow(xx): return xx**ipow
# xfunk.py
import inspect, funk
exec inspect.getsource(funk)
xx = 4.0
print xpow(xx)
ipow = 3
print xpow(xx)
And if you want trace information and be able to step through in a
debugger, you can use:
# funk.py
ipow = 2
def xpow(xx):
xx = xx / 0
return xx**ipow
# xfunk.py
import inspect, funk
exec compile(inspect.getsource(funk), inspect.getabsfile(funk),
'exec')
xx = 4.0
print xpow(xx)
regards,
Hung Jung
More information about the Python-list
mailing list