Loading video player…

Using Global Variables in Functions

00:00 Using Global Variables in Python Functions. Global variables are those that you can access and modify from anywhere in your code. In Python, you’ll typically defined global variables at the module level, so the containing module is their scope.

00:17 As you’ll see later on, You can also define global variables inside functions. Once you’ve defined a global variable, you can use it from within the module itself or from within any other modules in your code.

00:29 You can also use global variables in your functions. However, those cases can get a bit confusing because of differences between accessing and modifying global variables in functions.

00:41 To understand these differences, consider that Python can look for variables in four different scopes. The local or function level scope, which exists inside functions, the enclosing or non-local scope, which appears in nested functions.

00:57 The global scope, which exists at the module level and the built-in scope, which is a special scope for Python’s built-in names.

01:07 Let’s say you are inside an inner function. In that case, Python can look for names in all four scopes. When you access a variable in that inner function, Python first looks inside the function.

01:18 If the variable doesn’t exist there, then Python continues with the enclosing scope of the outer function. If it’s not defined there either, then Python moves to the global and then built-in scopes in that order.

01:31 If Python finds the variable, then you get the value back. Otherwise, you get a name error. When you launch an interactive session, it starts off at the module level of global scope.

01:44 In the example on screen, you have outer_func, which defines inner_func as a nested function.

01:59 From the perspective of this nested function, its own code block represents the local scope, while the outer_func code block before the call to inner_func represents the non-local or enclosing scope, enclosing inner_func.

02:15 If you call outer_func without defining some variable in either of the current scopes, then you get a name error exception because the name isn’t defined. As you can see on screen, if you define some variable in the global scope and then call outer_func, then you get the value returned on screen.

02:36 Internally, Python has searched the local, enclosing, and global scopes to find some variable and print its content. Note that you can define the variable in any of the three scopes, and Python will find it.

02:50 This search mechanism makes it possible to use global variables from inside functions. While taking advantage of this feature, you can face a few issues. For example, accessing a variable works, but directly modifying a variable doesn’t.

03:17 The access_number function works fine. It looks for number and finds it in the global scope.

03:27 But in contrast, modify_number doesn’t work as expected.

03:34 Why doesn’t this function update the value of the global variable number?

03:40 The problem is the scope of the variable. You can’t directly modify a variable from a higher level scope, such as global, in a lower level scope such as local.

03:51 Internally, Python assumes that any name directly assigned within a function is local to that function. Therefore, the local name number shadows its global sibling.

04:03 In this sense, global variables behave as read-only names. You can access their values, but you can’t modify them. The discussion about modifying global variables inside function revolves around assignment operations rather than in-place mutations of mutable objects. You’ll learn about the effects of mutability on global variables later on in the course.

04:26 Getting an unbound local error exception is another common issue you see when you try to modify a global variable inside a function. Consider the function on screen that attempts to use some global variables.

04:45 Inside print_globals, you first print the global variables A, B, and C. Then you update the value of C directly.

04:53 Finally, you print the updated version of C. You may be expecting the output that you can see on screen, but in practice, this isn’t what happens.

05:06 Instead, you get an error, which may surprise you. The problem is that the assignment of C = 100 creates a new local variable that overrides the original global variable C, so you have a name conflict.

05:21 The exception comes from the first call to print because at that time, the local version of C isn’t defined yet, so you get the UnboundLocalError.

05:32 In practice, this issue most often occurs in augmented assignment operations with global variables.

05:47 In Python, if your function simply references a variable from the global scope, then the function assumes that the variable is global. If you assign the variable’s name anywhere within the function’s body, then you define a new local variable with the same name as the original global. Inside a function, you can’t access a global variable directly if you defined local variables with the same name anywhere in the function.

06:15 In the next section of the course, you’ll further your understanding of this subject by looking at how to modify a global variable inside a function.

Become a Member to join the conversation.