471,319 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Question about functions

Question about functions:

1)--- module "A" contains the following
X = 999666 #easy to notice value
Y = 111222 #easy to notice value
Product = 69696969 #easy to notice value

def DoSomething():
global Product,X,Y
Product = X*Y
return 0

module imported
from A import *

2)--- toplevel code

def Mult():
global Product,X,Y
DoSomething():
return 0

"""start of top level code """
...
...
Product =Mult()
...
print Product ( Result ----> print out is 69696...)

Per "Python Nutshell" this appears to be the correct result.
How do you or can you modify the imported variables without using
classes objects?

Aubrey
Jul 18 '05 #1
2 1278
Hi,
don't use the form "from A import"
instead use the form " import A" and then use A.Product to modify the
"Product" variable.
Ahmed

"Aubrey Hutchison" <ab*****@comcast.net> wrote in message
news:I_********************@comcast.com...
Question about functions:

1)--- module "A" contains the following
X = 999666 #easy to notice value
Y = 111222 #easy to notice value
Product = 69696969 #easy to notice value

def DoSomething():
global Product,X,Y
Product = X*Y
return 0

module imported
from A import *

2)--- toplevel code

def Mult():
global Product,X,Y
DoSomething():
return 0

"""start of top level code """
...
...
Product =Mult()
...
print Product ( Result ----> print out is 69696...)

Per "Python Nutshell" this appears to be the correct result.
How do you or can you modify the imported variables without using
classes objects?

Aubrey

Jul 18 '05 #2

"Aubrey Hutchison" <ab*****@comcast.net> wrote in message
news:I_********************@comcast.com...
Question about functions:
It is not clear what your question is. Some comments anyway:
1)--- module "A" contains the following
X = 999666 #easy to notice value
Y = 111222 #easy to notice value
Product = 69696969 #easy to notice value
Should this just be "Product = X*Y' ?
def DoSomething():
global Product,X,Y
global Product # is sufficient since X,Y are read only
Product = X*Y
return X*Y # might be better
return 0
Omit: better to allow default return of None when there is no real return
value
module imported
I presume this is a comment missing #
from A import *
and that this belongs with the toplevel code below. But you almost
certainly do not want to import everything. 'import A' or 'from A import
DoSomething'.
2)--- toplevel code

def Mult():
global Product,X,Y
omit since not used in this function
DoSomething():
return 0
Again, no return. Better would simply be 'Mult = DoSomething'
"""start of top level code """
...
...
Product =Mult()
...
print Product ( Result ----> print out is 69696...)

Per "Python Nutshell" this appears to be the correct result.
How do you or can you modify the imported variables without using
classes objects?
You have not used any class objects, so I do not understand question. In
any case, you can only modify mutable objects. Otherwise, you can only
rebind name in one namespace or another. You must keep module A namespace
and main module namespace distinct in your mind since interpreter does.

Terry J. Reedy

Aubrey

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

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.