global keyword creates or references a global variable that can be updated within the scope of a function or class.
variable = initial_value def function_name: global variable # ... class ClassName: global variable def example_method: global variable # ...
The snippet above showcases the various places that a
variable can be referenced with the
variable must not be defined and set within the function/class scope before being referenced with the
global keyword. Otherwise, an error will occur.
The following is an example of the
global keyword being used in an
Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.
Learn Python on Codecademy
Computer ScienceLooking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Includes 6 Courses
With Professional Certification