I'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.
-
1If you use functions, but make their variables global, you haven't actually gained much ;-)user395760– user3957602012-12-27 08:57:39 +00:00Commented Dec 27, 2012 at 8:57
-
5"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.Rik Poggi– Rik Poggi2012-12-27 08:58:50 +00:00Commented Dec 27, 2012 at 8:58
-
No, I will not make all their variables global, probably just a few.user1396297– user13962972012-12-27 08:58:56 +00:00Commented Dec 27, 2012 at 8:58
-
3So what? Still neither necessary nor good. Use return values and parameters.user395760– user3957602012-12-27 09:00:28 +00:00Commented Dec 27, 2012 at 9:00
-
2Can 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.Alex L– Alex L2012-12-27 09:04:33 +00:00Commented Dec 27, 2012 at 9:04
5 Answers
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()`
8 Comments
10. You passing the variable x which has the value 10 to the other_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.x = 10. We then ran other_function(x), which means that we're passing the value of x (10) into other_function. In other_function the parameter is called parameter, rather than x, but it still has the value 10. Inside other_function we return parameter + 5, which is 10 + 5 = 15!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 do result = add(5, 6), and result would now equal 11. (Assuming add(x,y): return x+y)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
3 Comments
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`
Comments
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.
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.