Modifying a Global Variable in a Function
00:00 Modifying a Global Variable Inside a Function If you want to modify a global variable when inside a function, then you need to explicitly tell Python to use the global variable rather than creating a new local one.
00:14
There are two ways to do this, the global
keyword and the built-in globals()
function. Here, you’ll learn how to use both tools in your code to change global variables from inside your functions.
00:28 You’ve already learned that accessing global variables directly from inside a function is completely possible unless you have local variables with the same name in the containing function.
00:38
Apart from that, what if you also need to change the variable’s value? In that case, you can use the global
keyword to declare that you want to refer to the global variable.
00:49
The general syntax to write a global
statement is seen on screen. Note that the first variable is required while the rest of the variables are optional.
00:59 To illustrate how you can use this statement, think about the counter and increment example. You can fix the code as seen on screen.
01:13
In this new version of increment, you use the global
keyword to declare that you want the function to update the global variable counter
. With this statement, you are now able to modify the value of counter
inside the function as you can see on screen.
01:37 It’s important to note that you need to do the global declarations before you use the target variable. Otherwise, you’ll get a syntax error.
01:51
Here, you try to increment counter
without declaring it as a global inside increment
, and therefore you get a syntax error. The global
statement tells the Python interpreter when you find the name counter
in this function, it refers to the global variable, so don’t create a local one.
02:10
In summary, you only need to use the global
keyword if you want to reassign the global variable within a function. If you just need to read or access the value of the global variable, then you don’t need the global
keyword.
02:24
Python also has a non-local
keyword that works similarly to global
. This allows you to use non-local variables from nested functions.
02:33 Non-local variables are those that you define in a function that holds nested functions. These variables will be non-local to the inner or nested functions.
02:44
The built-in globals()
function allows you to access the global scope’s name table, which is a writable dictionary containing your current global names and their corresponding values.
02:55 You can use this function to access or modify the value of a global variable from within your functions. This function comes in handy when you have local variables with the same name as your target global variables, and you still need to use the global variable inside the function.
03:19
In this new implementation of print_globals()
, c
refers to the local version of the variable, while globals()['c']
refers to the global version.
03:37
Because globals()
returns a dictionary, you access its keys as you’d access the keys of any other dictionary. Note that you need to use a variable’s name as a string to access the corresponding key.
03:48
In globals()
, the dictionary of names that globals()
returns is mutable, which means that you can change the value of existing global variables by taking advantage of this dictionary.
04:00
In this example, you can see how the global variable C
still holds the same value.
04:08 Here’s another version of increment. It’s a little bit harder to read, but it does work.
04:22
Here you’ve implemented increment
by using globals()
instead of the global
keyword. Both implementations work the same, which you can confirm from the content of counter
after consecutive calls to the function.
04:37
Note that using globals()
to access and update global variables can make your code difficult to read and understand. Particularly for large programs, it’s generally better to use the global
keyword unless you have local variables with the same name as your target globals.
04:56 In the next section of the course, you’ll see how mutability affects global variables.
Become a Member to join the conversation.