passing global data to a function

B

beliavsky

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)

Running Python against xfunk.py, I get the output

16.0
16.0,

so the value of ipow in function xpow is not being changed. I want ipow to equal
4 in the 2nd call to xpow, so that the output would be

16.0
64.0

How can this be done? Thanks.
 
H

Hans Nowak

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)

Try:

import funk

xx = 4.0
print funk.xpow(xx)
funk.ipow = 3
print funk.xpow(xx)

Of course, there might be a better way to do this than with globals... a class
comes to mind as one possible alternative.

HTH,
 
B

Bengt Richter

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)

Running Python against xfunk.py, I get the output

16.0
16.0,

so the value of ipow in function xpow is not being changed. I want ipow to equal
4 in the 2nd call to xpow, so that the output would be

16.0
64.0

How can this be done? Thanks.

Not nice to pass args via globals, but
(oops, extra blank lines at end of funk.py):
----
ipow = 2
def xpow(xx): return xx**ipow


---- 64.0

IOW, if you use
from funk import xpow, ipow
you get local bindings of the names xpow and ipow
and when you call xpow(xx) you are accessing funk.xpow,
but when you assign ipow = 3 you are just changing the local
binding of ipow from what it was (same as funk.ipow) to a new value 3,
which doesn't affect funk.ipow, which is what xpow is using.
So you have to rebind the ipow name in xpow's global name space,
hence funk.ipow = 3 does what you wanted.

But you probably ought to find a better way to design this functionality.

Regards,
Bengt Richter
 
T

Terry Reedy

How can I pass global data to function stored in a separate file?

Since 'global data' (and more generally 'context data') is data that
is *not* passed to a function (but is instead read from the function's
context), your question is a contradiction;-). What you have run into
is the difference between lexical context (where the function is
defined) and dynamic context (where the function is called). Dynamic
scoping, which you were expecting or at least hoping for has been
tried in some languages (some dialects of Lisp, at least) but I
believe that experience has shown lexical scoping is overall
preferable.

As others have said, your immediate solution is to push the 'global'
value into the functions lexical context with 'funk.ipow = 3'.

Terry J. Reedy
 
D

David MacQuigg

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)

Running Python against xfunk.py, I get the output

16.0
16.0,

so the value of ipow in function xpow is not being changed. I want ipow to equal
4 in the 2nd call to xpow, so that the output would be

16.0
64.0

I found the scoping rules confusing also. In case anyone is writing a
text, here is a simpler example:

def fonc(): return p
# from funk import fonc # same def, but in module funk.py
# import funk
p = 2; print fonc()
p = 3; print fonc()
### RESULTS from uncommenting line #N, where #N =
#1) As expected.
#2) 'p' is not defined.
#3) 'fonc' is not defined

The variable 'p' trickles down ONLY to definitions within the same
module. ( "Lexical scoping", as explained by Terry Reedy. ) I'm not
sure why this is the right thing to do, but after a few months of
working with Python, my confidence is still growing that the designers
made the right choices. This is a superb language!! Perfect for a
technical professional like myself who is not a programmer.

Anyway, I have no complaint about this limitation. I agree with Bengt
Richter, this "back door" is not a good way to pass parameters into a
function.

Now if only we could simplify the scoping rules by having global
definitions pass down through nested classes the same as they do
through nested functions ... :>)

-- Dave
 
H

Hung Jung Lu

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top