In Python functions, an important concept to understand is scope. Variables declared (i.e. created) outside a function, in the body of the code, have global scope. By contrast, if the variable is declared inside a function, it has a local scope.
Variables with local scope, can only be accessed inside the function in which they were created. On the other hand, variables with a global scope can be accessed anywhere in the program, and in all functions of the program. Let’s look at an example:
def citylover(): sentence = "I love London!" # Assumed to be a new local variable ‘sentence’ print(sentence) sentence = "I love Paris!" # This is a global variable citylover() # This prints the value of the local variable print(sentence) # Prints value of the global variable
I love London! I love Paris!
Here, we have two different ‘sentence’ variables. There is the local variable which is created inside the function ‘citylover()’. Then, there is the global variable which is created outside the function. When the function ‘citylover()’ runs, it creates a local variable called ‘sentence’, gives it a value, and prints that value, "I love London!". Outside the function, we create a global variable with the same name, "sentence." However, as a local variable is created/modified in the function, rather than the global variable, it’s value doesn’t change. So, we still get "I love Paris!" when we print out the global variable "sentence."
Python assumes that all variables that are assigned to a value in a function are local variables. This makes coding in Python much easier, but, many times, we want to edit the global value rather than create a new local variable. To do this, we must use the ‘global’ keyword, before the variable name in the function, like so:
def citylover(): global sentence # We tell the function we want to work... #...with the global variable. sentence = "I love London!" # Now, the global variable is edited. print(sentence) sentence = "I love Paris!" # Global variable is created citylover() # This function edits the value to "I love London!" and prints it out. print(sentence) # Prints out the value of the global variable again
I love London! I love London!
Inside the function, we specify that we want to work with the global variable "sentence" inside the function. In one function, you can only use one type of variable: either local or global. After specifying the scope of the variable, we assign the variable to a new value, "I love London!". Then, we print that value.
Outside the function, we begin the program by creating a global variable "sentence" with a value "I love Paris!". Then, we call the function "citylover()" which changes the value of the "sentence" global variable, and prints it out, giving us our first output line, "I love London!". Then, we also print the value of global variable "sentence" outside the function. We get the same value "I love London!" because this time, the function actually changed the global variable, changing the value of "sentence" throughout the code.