Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

The global Declaration

00:00 What if you really need to modify a value in the global scope from within a function? This is possible in Python using the global declaration. In this example, the global x statement indicates that while the function executes, references to the name x refer to the x in the global namespace.

00:28 That means that when the value 40 is assigned to x after the local x statement, the interpreter doesn’t create a new reference in the local namespace of the function.

00:42 Instead, it assigns a new value to x in the global namespace. This example uses the equals specifier (=) for f-strings, which is added in Python 3.8.

00:56 So, when you load the module and run the function, you will see that x is not added to the local namespace, and it has the value 40.

01:10 And now when you call x, which is in the global namespace, you will see that its value has indeed been changed to 40.

01:23 As you have seen in an earlier lesson, the globals() function returns a reference to the global namespace dictionary. That means you can just reassign a value to a variable in the global scope directly through the dictionary that is returned by the globals() function, as you will see when you run the function and look at the current value of x. There isn’t much reason to do it this way, since the global declaration definitely makes the intent more clear.

01:57 Since a variable can be reassigned through the global namespace dictionary that is returned by the globals() function and using the global declaration has a similar behavior, you can also declare a new global variable within the function.

02:14 So when you try to call y before calling the function, you will see it doesn’t exist yet in the global namespace. However, when you call the function and then call y, you will see it is indeed created in the global namespace.

02:37 Just like you can with a lot of things in Python, it is also possible to specify several names in a single global declaration.

02:50 One thing to look out for when using the global declaration is that it appears in the function before it is used. In this function, the value of x is printed before the global declaration, which will throw a SyntaxError.

03:13 In this lesson, you learned about changing variables in the global scope. But what if you want to change variables in an enclosed scope? That is what you will learn in the next lesson.

Become a Member to join the conversation.