Question about functions

A

Aubrey Hutchison

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
 
A

Ahmed MOHAMED ALI

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
 
T

Terry Reedy

Aubrey Hutchison said:
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

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
 

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

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top