Which is the correct use of global variables in Python 3?:
1) Stating global VAR_NAME once in the core script (not within a function) and then simply referring to the variable as VAR_NAME everywhere else
2) Stating global VAR_NAME once within every function that uses the global variable and then simply referring to the variable as VAR_NAME for the rest of the function and within the core script itself
In the first case the global keyword is pointless, so that is not correct. Defining a variable on the module level makes it a global variable, you don't need the global keyword.
The second example is correct usage.
However, the most common usage for global variables are without using the global keyword anywhere. The global keyword is needed only if you want to reassign the global variables in the function/method.
You need to use the global keyword in a function if you use the global variable in a way that would otherwise be interpreted as an assignment to a local variable. Without the global keyword, you will create a local variable that hides the global in the scope of the function.
Here are a few examples:
global_var = 1
def example1():
# global keyword is not needed, local_var will be set to 1.
local_var = global_var
def example2():
# global keyword is needed, if you want to set global_var,
# otherwise you will create a local variable.
global_var = 2
def example3():
# Without using the global keyword, this is an error.
# It's an attempt to reference a local variable that has not been declared.
global_var += 1
"in a way that would otherwise be interpreted as an assignment to a local variable" --- yes, but here is a subtle detail:
------------------- error: local variable 'c' referenced before assignment
def work():
c += 3
c = 0
work()
print(c)
------------------- error: local variable 'c' referenced before assignment
c = 0
def work():
c += 3
work()
print(c)
------------------- prints [3]
def work():
c.append(3)
c = []
work()
print(c)
------------------- prints [3]
c = []
def work():
c.append(3)
work()
print(c)
The main difference between the first two cases and the next two cases in the above answer would have to be the fact that the list is mutable. For cases like a = 1 a pointer points to the location where 1 is and when you say a = 2 the pointer shifts.
For the case of mutable objects a memory location is allotted and when methods like append are used changes occur to the memory location itself and so the value the mutable references is changed globally.
Now the big question is as to how the function knows the variable we are modifying is a global one or local one because it seems we can modify the global variable if its mutable and we cannot if its non mutable (The function also does not recognize this as the global variable)
Related
I made the two functions below in Python 3. The first function test_list works fine with the list a without error. I can modify the list element in that function.
However, the second funciton test_int will pop an error local variable 'b' referenced before assignment. Why can't I do this to the variable b?
a=[1,2,3]
def test_list():
a[0]=2
return a
b = 2
def test_int():
b += 1
return b
b += 1 is equivalent to b = b.__iadd__(1); since the name b gets bound by that statement (l.h.s.), the compiler infers that b is a local variable; however, the r.h.s. expression contains the name b as well and thus you get the error "local variable 'b' referenced before assignment" (the local name b doesn't refer to anything at the time when the expression is evaluated).
a[0] = 2 on the other hand is equivalent to a.__setitem__(0, 2) and thus involves no name binding.
If you wish to modify a global name binding within a function, you can use the keyword global; there's also the equivalent nonlocal for names contained in an outer, but not the global, scope.
b is globally scoped. You are attempting to modify it in a local context, ie in test_int. You need the global keyword if you wish to modify a globally scoped variable in a local context. Read about variable scoping in python here.
The reason the first function test_list works is because you are not modifying the global variable itself, but only the contents. Here's a good explanation of what happens when you modify global variables: https://stackoverflow.com/a/31437415/14715054
This will fix your problem:
a=[1,2,3]
def test_list():
a[0]=2
return a
b = 2
def test_int():
global b
b += 1
return b
This question already has answers here:
Using global variables in a function
(25 answers)
Closed 5 months ago.
I know I should avoid using global variables in the first place due to confusion like this, but if I were to use them, is the following a valid way to go about using them? (I am trying to call the global copy of a variable created in a separate function.)
x = "somevalue"
def func_A ():
global x
# Do things to x
return x
def func_B():
x = func_A()
# Do things
return x
func_A()
func_B()
Does the x that the second function uses have the same value of the global copy of x that func_a uses and modifies? When calling the functions after definition, does order matter?
If you want to simply access a global variable you just use its name. However to change its value you need to use the global keyword.
E.g.
global someVar
someVar = 55
This would change the value of the global variable to 55. Otherwise it would just assign 55 to a local variable.
The order of function definition listings doesn't matter (assuming they don't refer to each other in some way), the order they are called does.
Within a Python scope, any assignment to a variable not already declared within that scope creates a new local variable unless that variable is declared earlier in the function as referring to a globally scoped variable with the keyword global.
Let's look at a modified version of your pseudocode to see what happens:
# Here, we're creating a variable 'x', in the __main__ scope.
x = 'None!'
def func_A():
# The below declaration lets the function know that we
# mean the global 'x' when we refer to that variable, not
# any local one
global x
x = 'A'
return x
def func_B():
# Here, we are somewhat mislead. We're actually involving two different
# variables named 'x'. One is local to func_B, the other is global.
# By calling func_A(), we do two things: we're reassigning the value
# of the GLOBAL x as part of func_A, and then taking that same value
# since it's returned by func_A, and assigning it to a LOCAL variable
# named 'x'.
x = func_A() # look at this as: x_local = func_A()
# Here, we're assigning the value of 'B' to the LOCAL x.
x = 'B' # look at this as: x_local = 'B'
return x # look at this as: return x_local
In fact, you could rewrite all of func_B with the variable named x_local and it would work identically.
The order matters only as far as the order in which your functions do operations that change the value of the global x. Thus in our example, order doesn't matter, since func_B calls func_A. In this example, order does matter:
def a():
global foo
foo = 'A'
def b():
global foo
foo = 'B'
b()
a()
print foo
# prints 'A' because a() was the last function to modify 'foo'.
Note that global is only required to modify global objects. You can still access them from within a function without declaring global.
Thus, we have:
x = 5
def access_only():
return x
# This returns whatever the global value of 'x' is
def modify():
global x
x = 'modified'
return x
# This function makes the global 'x' equal to 'modified', and then returns that value
def create_locally():
x = 'local!'
return x
# This function creates a new local variable named 'x', and sets it as 'local',
# and returns that. The global 'x' is untouched.
Note the difference between create_locally and access_only -- access_only is accessing the global x despite not calling global, and even though create_locally doesn't use global either, it creates a local copy since it's assigning a value.
The confusion here is why you shouldn't use global variables.
You can directly access a global variable inside a function. If you want to change the value of that global variable, use "global variable_name". See the following example:
var = 1
def global_var_change():
global var
var = "value changed"
global_var_change() #call the function for changes
print var
Generally speaking, this is not a good programming practice. By breaking namespace logic, code can become difficult to understand and debug.
As others have noted, you need to declare a variable global in a function when you want that function to be able to modify the global variable. If you only want to access it, then you don't need global.
To go into a bit more detail on that, what "modify" means is this: if you want to re-bind the global name so it points to a different object, the name must be declared global in the function.
Many operations that modify (mutate) an object do not re-bind the global name to point to a different object, and so they are all valid without declaring the name global in the function.
d = {}
l = []
o = type("object", (object,), {})()
def valid(): # these are all valid without declaring any names global!
d[0] = 1 # changes what's in d, but d still points to the same object
d[0] += 1 # ditto
d.clear() # ditto! d is now empty but it`s still the same object!
l.append(0) # l is still the same list but has an additional member
o.test = 1 # creating new attribute on o, but o is still the same object
Here is one case that caught me out, using a global as a default value of a parameter.
globVar = None # initialize value of global variable
def func(param = globVar): # use globVar as default value for param
print 'param =', param, 'globVar =', globVar # display values
def test():
global globVar
globVar = 42 # change value of global
func()
test()
=========
output: param = None, globVar = 42
I had expected param to have a value of 42. Surprise. Python 2.7 evaluated the value of globVar when it first parsed the function func. Changing the value of globVar did not affect the default value assigned to param. Delaying the evaluation, as in the following, worked as I needed it to.
def func(param = eval('globVar')): # this seems to work
print 'param =', param, 'globVar =', globVar # display values
Or, if you want to be safe,
def func(param = None)):
if param == None:
param = globVar
print 'param =', param, 'globVar =', globVar # display values
You must use the global declaration when you wish to alter the value assigned to a global variable.
You do not need it to read from a global variable. Note that calling a method on an object (even if it alters the data within that object) does not alter the value of the variable holding that object (absent reflective magic).
When I code games with functions, I often get confused as to which variable to global. I've heard that globalizing variables isn't a very practice, so I try to minimize the amount by not globalizing any, and only globalize the ones that the error message tells me to. But doing that is quite annoying, and it wastes time. Can someone tell me the rule of thumb as to when we should global a variable in a function, and when it is not necessary? Here is a sample of what I mean (the functions):
import turtle
from random import randint as rd
from time import sleep
delay = 0.1
wn = turtle.Screen()
wn.setup(400,400)
wn.tracer(0)
player = turtle.Turtle('square')
player.penup()
player.goto(0,-170)
rock = turtle.Turtle('circle')
rock.shapesize(0.5,0.5)
rock.penup()
rock.goto(rd(-190,190),200)
rocks = [rock]
pen = turtle.Turtle(visible=False)
pen.penup()
pen.goto(0,150)
def go_left(): # No globalizing here
if player.xcor() >= -170:
player.setx(player.xcor()-10)
def go_right(): # No globalizing here
if player.xcor() <= 170:
player.setx(player.xcor()+10)
def move_rocks(): # No globalizing here
for rock in rocks:
rock.sety(rock.ycor()-rd(0,2))
def loop_rocks():
global count # rocks not globalized here
for rock in rocks:
if rock.ycor() < -200:
count += 1
rock.goto(rd(-190,190),200)
def add_rocks():
global count # rocks not globalized here
if count == len(rocks) and len(rocks) <= 15:
rock = turtle.Turtle('circle')
rock.shapesize(0.5,0.5)
rock.penup()
rock.goto(rd(-190,190),200)
rocks.append(rock)
count = 0
def write_score():
global time,score,high_score # pen not globalized here
time += 1
if time == 500:
score += 1
time = 0
if score > high_score:
high_score = score
pen.clear()
pen.write(f'Score: {score} High Score: {high_score}',align='center',font=('Arial',10,'bold'))
def hit(): # No globalizing here
for rock in rocks:
if player.distance(rock) < 15:
return True
def die():
global score,rocks # player not globalized here
sleep(1)
for rock in rocks:
rock.goto(300,300)
rocks = rocks[:1]
rocks[0].goto(rd(-190,190),200)
player.goto(0,-170)
score = 0
wn.listen()
wn.onkeypress(go_left,'Left')
wn.onkeypress(go_right,'Right')
score = 0
high_score = 0
count = 0
time = 0
while True:
if hit():
die()
move_rocks()
loop_rocks()
add_rocks()
write_score()
wn.update()
Style rules are not language rules. I.e. you shouldn't use eval(), but there it is, in the language.
tell me the rule of thumb as to when we should global a variable in a
function, and when it is not necessary?
The rules for when, and when not, to use global are simple, but even tutorials on the web get it wrong.
The global keyword should not be used to create a global
variable.
(Yes, that's partly a style rule.) When you define a top level variable outside a function, Python makes it global. (You don't use the global keyword for this.) When you assign to a variable inside a function, Python assumes it is local to the function. You only need the global keyword when you want change that later assumption so you can reassign (=) a global variable from within a function. You don't need the global declaration to examine a global variable. You don't need it to invoke a method on a global variable that might change its internal state or content:
You only need the global keyword when you want to reassign (=) a
global variable within a function.
The global declaration is used in any function where a global variable is reassigned. It is is placed ahead of the first reference to the variable, access or assignment. For simplicity, and style, global statements are put at the beginning of the function.
A statement like, "You should never use global variables", is a style rule and true of most programming languages -- apply it if/when you can. And if you absolutely can't, don't feel bad about it, just:
Comment all globals you do use properly.
Global constants are less an issue:
If global constants are truly constant, they never need the global
keyword.
#juanpa.arrivillaga's example of go_left() taking the additional values as parameters instead of global variables, fails to take into account that go_left() is a callback and that the turtle event assignment functions don't provide for additional parameters. (They should, but they don't.) We can get around this using a lambda expression (or partial function from functools), but when used this way, lambda isn't particularly great style either, IMHO.
#martineau's suggestion of "making them attributes of a class that the class' methods can access" (aka class variables) is fine, but what is left unsaid is that it means subclassing Turtle or wrapping a turtle instance with another class.
My personal issue with mutable globals is that they are problematic in a multi-threaded world.
Although it is not an answer, I just wanted to point out one more thing to look out for when shadowing names from outer scopes / global variables. cdlane writes in their answer that
You don't need the global declaration to examine a global variable.
I think it goes even further than that, because you cannot use the global keyword that way, as it is a declaration. As cdlane already said, it is used to declare variables in a local scope (such as a function or class) to be of global scope, such that you can assign new values to these variables from a local scope. You can even use the gobal keyword to declare new global variables from a local scope, although again, as cdlane pointed out, this is not a good idea. Here is some code highlighting these behaviours:
a = c = 1 # define global variables 'a' and 'b' and assign both the
# value 1.
class Local:
def __init__(self):
print(a) # if you only want to examine the global variable 'a',
# you don't need any keywords
self.declare_variables()
def declare_variables(self):
global a, b # declare 'a' and 'b' as global variables, such that any
# assignment statements in this scope refer to the global variables
a = 2 # reassign a new value to the already existing global
# variable 'a'.
b = 3 # assign a value to the previously undeclared global variable
# 'b'. you should avoid this.
c = 4 # this does not affect the global variable 'c', since the
# variable 'c' in this scope is assumed to be local by default.
local = Local()
print(a, b, c) # the vaules of the gobal variables 'a' and 'b' have changed,
# while 'c' remains unaffected.
So far nothing really new. However, when you are shadowing the names from global variables, but are still accessing the global variables elsewhere in the same scope, this becomes a problem.
If you declare a variable shadowing the name of a global variable before you try to access that global variable, all references to that variable name following that declaration will refer to the local variable. I think this might be the worse case, since this could go undetected and not produce any errors, but return wrong results.
If you try to declare a new local variable, or use the global keyword with the same variable name after you have already referenced that variable name in the same scope, it will result in an UnboundLocalError or SyntaxError, respectively.
def reference_global_before_local_declaration():
print(a) # first reference the global variable 'a'. this statement would
# work on its own if 'a' wouldn't be redeclared to be a local variable
# later on.
a = 5 # redeclare 'a' to be a local variable and assign it the value 5.
reference_global_before_local_declaration()
def reference_global_before_global_declaration():
print(a) # first reference the global variable 'a'. this statement would
# work on its own if 'a' wouldn't be declared to be a global variable
# again later on.
global a # declare 'a' to be a global variable again.
reference_global_before_global_declaration()
def reference_global_after_local_declaration():
a = 'text' # redeclare 'a' to be a local variable of type string and
# assign it the value 'text'.
b = a + 1 # here, 'a' was supposed to reference the global variable
# 'a', but is now referencing the local variable 'a' instead, due to 'a'
# being declared in the same scope and shadowing the name of the gobal
# variable 'a'.
reference_global_after_local_declaration()
The only way that I know of to avoid this, is to use the globals() function, although this really defeats all purpose and I wouldn't recommend it. I would however recommend to read PEP 3104 - Access to Names in Outer Scopes, which discusses these kinds of problems and presents a solution, which was ultimately never implemented though.
def reference_global_before_local_declaration():
print(globals()['a'])
a = 5
reference_global_before_local_declaration()
def reference_global_before_global_declaration():
print(globals()['a'])
global a
reference_global_before_global_declaration()
def reference_global_after_local_declaration():
a = 'text'
b = globals()['a'] + 1
reference_global_after_local_declaration()
I have the following snippet:
def test ():
num_sum = 0
def inner_test ():
global num_sum
num_sum += 1
inner_test()
return num_sum
When I run test() I get:
NameError: name 'num_sum' is not defined
I was expecting that the inner function would change the value of the num_sum variable defined in the outer function. Basically, I need a global variable to increment in an inner function which I may call recursively.
I noticed that this pattern works well with collections (lists, dictionaries) even if I don't define the variable as global (but pass it as a parameter to the inner function).
However, with scalar values like int this pattern seems to break. Neither defining the variable as global (as is here) nor passing it as a parameter to the inner function works as intended. Basically, the scalar variable stays unchanged. What do I need to do to get the desired behaviour with such scalar values?
you need nonlocal instead of global. your num_sum is not a global variable (will not be found in globals()). nonlocal will instruct python not to search for it in the global namespace, but in the nearest namespace. the order is LEGB: Local, Enclosed, Global, Built-in.
This question already has answers here:
Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope?
(9 answers)
Closed 5 months ago.
I am confused with the global keyword behavior in below code snippet, I was expecting 30, 30, 30 in all 3 prints.
def outer_function():
#global a ###commented intentionally
a = 20
def inner_function():
global a
a = 30
print('a =',a)
inner_function()
print('a =',a)
a = 10
outer_function()
print('a =',a)
#Output:
#30
#20 #Expecting 30 here
#30
All the confusion coming from "global a" after outer function definition. As my understanding at this point of time is " All the reference and assignment to variable become globally reflected on declaration of global keyword on that variable". If I am uncommenting that first global statement I am getting expected output 30,30,30.
Why global declaration inside inner_function and value change does not reflect on 2nd print i:e to outer_function(or outer scope), whereas got reflected in global namespace.
A common acronym to be familiar with is LEGB:
Local
Enclosed
Global
Built-in
This is the order in which Python will search the namespaces to find variable assignments.
Local
The local namespace is everything that happens within the current code block. Function definitions contain local variables that are the first thing that is found when Python looks for a variable reference. Here, Python will look in the local scope of foo first, find x with the assignment of 2 and print that. All of this happens despite x also being defined in the global namespace.
x = 1
def foo():
x = 2
print(x)
foo()
# prints:
2
When Python compiles a function, it decides whether each of the variables within the definition code block are local or global variables. Why is this important? Let's take a look at the same definition of foo, but flip the two lines inside of it. The result can be surprising
x = 1
def foo():
print(x)
x = 2
foo()
# raises:
UnboundLocalError: local variable 'x' referenced before assignment
This error occurs because Python compiles x as a local variable within foo due to the assignment of x = 2.
What you need to remember is that local variables can only access what is inside of their own scope.
Enclosed
When defining a multi-layered function, variables that are not compiled as local will search for their values in the next highest namespace. Here is a simple example.
x = 0
def outer_0():
x = 1
def outer_1():
def inner():
print(x)
inner()
outer_1()
outer_0()
# print:
1
When inner() is compiled, Python sets x as a global variable, meaning it will try to access other assignments of x outside of the local scope. The order in which Python searches for a value of x in moving upward through the enclosing namespaces. x is not contained in the namespace of outer_1, so it checks outer_0, finds a values and uses that assignment for the x within inner.
x --> inner --> outer_1 --> outer_0 [ --> global, not reached in this example]
You can force a variable to not be local using the keywords nonlocal and global (note: nonlocal is only available in Python 3). These are directives to the compiler about the variable scope.
nonlocal
Using the nonlocal keyword tells python to assign the variable to first instance found as it moves upward through the namespaces. Any changes made to the variable will be made in the variable's original namespace as well. In the example below, when 2 is assigned x, it is setting the value of x in the scope of outer_0 as well.
x = 0
def outer_0():
x = 1
def outer_1():
def inner():
nonlocal x
print('inner :', x)
x = 2
inner()
outer_1()
print('outer_0:', x)
outer_0()
# prints:
inner : 1
outer_0: 2
Global
The global namespace is the highest level namespace that you program is running in. It is also the highest enclosing namespace for all function definitions. In general it is not good practice to pass values in and out of variables in the global namespace as unexpected side effects can occur.
global
Using the global keyword is similar to non-local, but instead of moving upward through the namespace layers, it only searches in the global namespace for the variable reference. Using the same example from above, but in this case declaring global x tells Python to use the assignment of x in the global namespace. Here the global namespace has x = 0:
x = 0
def outer_0():
x = 1
def outer_1():
def inner():
global x
print('inner :', x)
inner()
outer_1()
outer_0()
# prints:
0
Similarly, if a variable is not yet defined in the global namespace, it will raise an error.
def foo():
z = 1
def bar():
global z
print(z)
bar()
foo()
# raises:
NameError: name 'z' is not defined
Built-in
Last of all, Python will check for built-in keywords. Native Python functions such as list and int are the final reference Python checks for AFTER checking for variables. You can overload native Python functions (but please don't do this, it is a bad idea).
Here is an example of something you SHOULD NOT DO. In dumb we overload the the native Python list function by assigning it to 0 in the scope of dumb. In the even_dumber, when we try to split the string into a list of letters using list, Python will find the reference to list in the enclosing namespace of dumb and try to use that, raising an error.
def dumb():
list = 0
def even_dumber():
x = list('abc')
print(x)
even_dumber()
dumb()
# raises:
TypeError: 'int' object is not callable
You can get back the original behavior by referencing the global definition of list using:
def dumb():
list = [1]
def even_dumber():
global list
x = list('abc')
print(x)
even_dumber()
dumb()
# returns:
['a', 'b', 'c']
But again, DO NOT DO THIS, it is bad coding practice.
I hope this helps bring to light some of how the namespaces work in Python. If you want more information, chapter 7 of Fluent Python by Luciano Ramalho has a wonderful in-depth walkthrough of namespaces and closures in Python.
From the documentation:
The global statement is a declaration which holds for the entire
current code block. It means that the listed identifiers are to be
interpreted as globals.
Note it only applies to current code block. So the global in inner_function only applies within inner_function. Outside of it, the identifier is not global.
Note how “identifier” is not the same as “variable”. So what it tells the interpreter is “when I use identifier a within this code block, do not apply normal scope resolution, I actually mean the module-level variable, ”.
Just uncomment your global command in the outer_function, otherwise you're declaring a local variable with value 20, changing a global variable then printing that same local variable.
It's not a good idea use global variabilities. If you want only reset the value of a variable, you just use this lines:
def outer_function():
a = 20
def inner_function():
a = 30
print('a =',a)
return a
a = inner_function()
print('a =',a)
return a
a = 10
a = outer_function()
print('a =',a)