How to make a local variable (inside a function) global
Solution 1
Here are two methods to achieve the same thing:
Using parameters and return (recommended)
def other_function(parameter):
return parameter + 5
def main_function():
x = 10
print(x)
x = other_function(x)
print(x)
When you run main_function
, you'll get the following output
>>> 10
>>> 15
Using globals (never do this)
x = 0 # The initial value of x, with global scope
def other_function():
global x
x = x + 5
def main_function():
print(x) # Just printing - no need to declare global yet
global x # So we can change the global x
x = 10
print(x)
other_function()
print(x)
Now you will get:
>>> 0 # Initial global value
>>> 10 # Now we've set it to 10 in `main_function()`
>>> 15 # Now we've added 5 in `other_function()`
Solution 2
Simply declare your variable outside any function:
globalValue = 1
def f(x):
print(globalValue + x)
If you need to assign to the global from within the function, use the global
statement:
def f(x):
global globalValue
print(globalValue + x)
globalValue += 1
Solution 3
If you need access to the internal states of a function, you're possibly better off using a class. You can make a class instance behave like a function by making it a callable, which is done by defining __call__
:
class StatefulFunction( object ):
def __init__( self ):
self.public_value = 'foo'
def __call__( self ):
return self.public_value
>> f = StatefulFunction()
>> f()
`foo`
>> f.public_value = 'bar'
>> f()
`bar`
Solution 4
Using globals will also make your program a mess - I suggest you try very hard to avoid them. That said, "global" is a keyword in python, so you can designate a particular variable as a global, like so:
def foo():
global bar
bar = 32
I should mention that it is extremely rare for the 'global' keyword to be used, so I seriously suggest rethinking your design.
Solution 5
You could use module scope. Say you have a module called utils
:
f_value = 'foo'
def f():
return f_value
f_value
is a module attribute that can be modified by any other module that imports it. As modules are singletons, any change to utils
from one module will be accessible to all other modules that have it imported:
>> import utils
>> utils.f()
'foo'
>> utils.f_value = 'bar'
>> utils.f()
'bar'
Note that you can import the function by name:
>> import utils
>> from utils import f
>> utils.f_value = 'bar'
>> f()
'bar'
But not the attribute:
>> from utils import f, f_value
>> f_value = 'bar'
>> f()
'foo'
This is because you're labeling the object referenced by the module attribute as f_value
in the local scope, but then rebinding it to the string bar
, while the function f
is still referring to the module attribute.
Related videos on Youtube
codeforester
Scripting and automation enthusiast. Fun fact: Code is the reason for bugs. More code = more bugs! Write reusable code!
Updated on July 08, 2020Comments
-
codeforester almost 4 years
Possible Duplicate:
Using global variables in a function other than the one that created themI'm using functions so that my program won't be a mess but I don't know how to make a local variable into global.
-
Admin over 11 yearsIf you use functions, but make their variables global, you haven't actually gained much ;-)
-
Rik Poggi over 11 years"I'm using functions so that my program won't be a mess", so just don't and refactor your code so that you wont need them.
-
Admin over 11 yearsNo, I will not make all their variables global, probably just a few.
-
Admin over 11 yearsSo what? Still neither necessary nor good. Use return values and parameters.
-
Admin over 11 years@mgisnobr, I'm doing opposite, I'm going to use local variables in the main program, not just the functions
-
Admin over 11 yearsI set up the variables in the function's parameters and the function changes them and I have to use them back in the main loop. I return the values but can't assign them to a variable.
-
CppLearner over 11 yearsIt's okay to use global variables, but making a local variable as global is odd. Just make it global once and for all. At the end of the time, your code can be import and is part of some global environment. Security wise, I don't really see anything wrong with using a plain global.
-
Alex L over 11 yearsCan you give us a short example of how you'd be using global values? (Edit your question) We might be able to suggest a better approach.
-
CppLearner over 11 years@HwangYouJin What do you mean you can't assign them back to a variable?
-
Admin over 11 yearsYes, I can't. Since when I return the value, the global variable does not change its value
-
Admin over 11 yearsThank you guys, already found a solution, I mean I found another way so that I will not need this anymore. How do you close a question?
-
Alex L over 11 years@HwangYouJin You should accept the answer that helped you the most :) Keep on asking questions and playing with Python, you're doing much better than most 13 year olds! (Boo to the downvoters)
-
Admin over 11 yearsOh, so you checked my profile. Well, you know I'm already 14 now, but then OK, I'll check the answer that made me learn something, or well, you know.
-
-
Admin over 11 yearsI have already used the global statement, but what I need is the current value of the global variable, which is in the function.
-
Greg Hewgill over 11 yearsYour request doesn't seem to make sense. The current value of the global variable is in the global variable. Perhaps you could show your code to ease understanding.
-
Admin over 11 yearsI just used a global variable because many of my functions share it and I don't want to assign lines of code to update every variable in each of the functions.
-
Admin over 11 yearsNo, the function supposedly should change the variable and return its value and update the global variable, but I can't seem to do that
-
Admin over 11 yearsWait, how did the other_function() in your first example get a value of 10? You did not assign a value to its parameter, so it will not be able to know that the value was 5.
-
CppLearner over 11 years@HwangYouJin The original value is
10
. You passing the variable x which has the value10
to theother_function
. It's like feeding into a machine. You give the machine a piece of bread (x
in this case). You want to make toast. You give it a bread. Now the machine has the bread so it can use the bread. I think you should ask more questions to clear up confusions you have. -
Alex L over 11 years@HwangYouJin We set x to 10 with
x = 10
. We then ranother_function(x)
, which means that we're passing the value of x (10) intoother_function
. Inother_function
the parameter is calledparameter
, rather than x, but it still has the value 10. Insideother_function
we returnparameter + 5
, which is 10 + 5 = 15! -
Alex L over 11 years@HwangYouJin Does this make more sense now?
-
Admin over 11 yearsAh...Ok... I already noticed you gave x its value before calling other_function()... sorry. Anyway, how do you turn a value returned from a function into a variable, or to say it otherwise, how do you give a variable a value which is the value returned by a function?
-
Alex L over 11 years@HwangYouJin Just as easy as
variable = function(parameter)
. This will run the function, pass the parameter, and receive the returned value and put it into variable. If you were building a calculator you could doresult = add(5, 6)
, andresult
would now equal 11. (Assumingadd(x,y): return x+y
) -
Norfeldt almost 11 yearsYour point would perhaps be more clear if you made an assignment in
other_function(parameter)
so you wroteparameter = 5
and returned thatreturn parameter
. Then inmain_function()
you should replacex = other_function(x)
withprint other_function(x)
. The print out would be 10, 5, 10 -
Jonathan over 6 years@Matthew_Trevor's answer has a third option that is sometimes needed in more advanced scenarios.