A little question about python variable scope
I am a beginner python and am wondering very confusing for me. If I define a function first, but inside a function, I have to use a variable that is defined in another function below, can I do it like this? Or how can I import the returned things of another function into a function? eg:
def hello(x,y):
good=hi(iy,ix)
"then do somethings,and use the parameter'good'."
return something
def hi(iy,ix):
"code"
return good
source to share
The scope of functions hello
and is hi
completely different. They don't have any variables.
Note that the call results in hi(x,y)
some object. You store this named object good
in a function hello
.
The variable named good
in hello
is another variable that is not associated with the variable named good
in the function hi
.
They are spelled the same, but they exist in different namespaces. To prove this, change the spelling of the variable good
in one of the two functions, you can see that it still works.
Edit. Follow-up observation: "so what should I do if I want to use the result of a function hi
in a function hello
?"
Nothing unusual. Take a look hello
.
def hello(x,y):
fordf150 = hi(y,x)
"then do somethings,and use the variable 'fordf150'."
return something
def hi( ix, iy ):
"compute some value, good."
return good
Some script is evaluating hello( 2, 3)
.
-
Python creates a new namespace for evaluation
hello
. -
The
hello
object isx
attached to the object2
. The linking is done in order. -
The
hello
object isy
attached to the object3
. -
The
hello
Python evaluates the first operatorfordf150 = hi( y, x )
,y
is 3,x
is equal to 2.and. Python creates a new namespace for evaluation
hi
.b. The
hi
object isix
attached to the object3
. The linking is done in order.from. The
hi
object isiy
attached to the object2
.In
hi
something happens and isgood
attached to some object, say3.1415926
.e. B
hi
holds areturn
; identifying the object as a value forhi
. In this case, the object is calledgood
and is an object3.1415926
.f. The namespace is
hi
discarded.good
,ix
andiy
disappear. However, the object (3.1415926
) remains as a score valuehi
. -
In
hello
, Python completes the first statementfordf150 = hi( y, x )
,y
is 3,x
is equal to 2. The value ofhi
the same3.1415926
.and.
fordf150
attached to the object created by calculatinghi
,3.1415926
. -
In
hello
Python, it goes to other statements. -
At some point
something
attached to an object, say2.718281828459045
. -
B
hello
is executed areturn
; identifying the object as a value forhello
. In this case, the object is calledsomething
and is an object2.718281828459045
. -
The namespace is discarded.
fordf150
andsomething
disappear, likex
andy
. However, the object (2.718281828459045
) remains as a score valuehello
.
Any program or script called hello
gets a response.
source to share
If you want to define a variable in the global namespace from within a function and thereby make it available to other functions in that function, you can use the global keyword. Here are some examples
varA = 5 #A normal declaration of an integer in the main "global" namespace
def funcA():
print varA #This works, because the variable was defined in the global namespace
#and functions have read access to this.
def changeA():
varA = 2 #This however, defines a variable in the function own namespace
#Because of this, it not accessible by other functions.
#It has also replaced the global variable, though only inside this function
def newVar():
global varB #By using the global keyword, you assign this variable to the global namespace
varB = 5
def funcB():
print varB #Making it accessible to other functions
Conclusion: Variables defined in a function remain in the function's namespace. It still has read-only access to the global namespace, unless the variable was called with the global keyword.
The term global is not completely global as it might seem at first glance. It is practically just a reference to the lowest namespace in the file you are working in. Global keywords are not available in another module.
As a mild warning, this may be considered less "good practice" for some.
source to share
your example program works because the two instances of "good" are different variables (you just have both variables with the same name). The following code is exactly the same:
def hello(x,y):
good=hi(iy,ix)
"then do somethings,and use the parameter'good'."
return something
def hi(iy,ix):
"code"
return great
source to share