473,401 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

passing global data to a function

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.
Jul 18 '05 #1
5 1648
be*******@aol.com wrote:
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,

--
Hans (ha**@zephyrfalcon.org)
http://zephyrfalcon.org/

Jul 18 '05 #2
On 1 Dec 2003 08:55:38 -0800, be*******@aol.com wrote:
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):
print '----\n%s\n----'%file('funk.py').read() ----
ipow = 2
def xpow(xx): return xx**ipow
---- import funk
xpow = funk.xpow
xx = 4.0
print xpow(xx) 16.0 funk.ipow = 3
print xpow(xx)

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
Jul 18 '05 #3

<be*******@aol.com> wrote in message
news:30************************@posting.google.com ...
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
Jul 18 '05 #4
On 1 Dec 2003 08:55:38 -0800, be*******@aol.com wrote:
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
Jul 18 '05 #5
be*******@aol.com wrote in message news:<30************************@posting.google.co m>...
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
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could...
4
by: hello smith | last post by:
I have a lot of functions that add values to an array. They alos update a global variable of type int. Currently, I use a global variable to hold this array. All functions access this array...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
20
by: MartinRinehart | last post by:
Is the following correct? x = "some string" x is a reference to "some string" foo(x) Reference is passed to function.
1
by: Dean Slindee | last post by:
VS2008, .NetFramework 3.5 SP1: I have built a LINQ data access layer project. When the LINQ data context was built over an existing SQL2005 database, the connection string for that database was...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.