Skip to main content

Python quick bite 01 - Scope

Summary of Scope (lesson learnt from 100 Days of Code, Day 12)

There is the local scope and global scope for variables. 

Global scope refers to variables that we assigned in our codes.

Local scope refers to variables that we assign or use within a function. 

In a function, if no variable is defined, the global scope version will be used. 

eg 1.

coffee_drink = "long black"

def cuppa():
   print("Drink inside cuppa function:", coffee_drink)

cuppa()
print("Drink outside cuppa function:", coffee_drink)

This will return you:

Drink inside cuppa function: long black
Drink outside cuppa function: long black

However, if you had defined a coffee_drink in the function, that coffee_drink is only callable within the function. 

eg 2. 

def cuppa():
    coffee_drink = "latte"
    print("Drink inside cuppa function:", coffee_drink)

cuppa()
print("Drink outside cuppa function:", coffee_drink)

This will return you:

Drink inside cuppa function: latte
Name error as the coffee_drink cannot be accessed outside the function cuppa(). 

eg 3. 

coffee_drink = "long black"

def cuppa():
    coffee_drink = "latte"
    print("Drink inside cuppa function:", coffee_drink)

cuppa()
print("Drink outside cuppa function:", coffee_drink)

This will return you:

Drink inside cuppa function: latte
Drink outside cuppa function: long black

Even though both variables are called coffee_drink but they are referring to a different cup of coffee. 

So it is a bad idea to call both the local variable and global variable by the same names since we are not referring to the same thing.

There is no block scope unlike in java or in C++ languages, meaning that in python, it is not counted as a local scope if it is within if, for or while loops.  

To can call upon global scope inside our function, we need to define the variable to mean the global variable explictly. 

eg 4. 

coffee_drink = "long black"

def cuppa():
    global coffee_drink 
    coffee_drink += " latte"
    print("Drink inside cuppa function:", coffee_drink)

cuppa()
print("Drink outside cuppa function:", coffee_drink)

This will return you:

Drink inside cuppa function: long black latte
Drink outside cuppa function: long black latte

So here the coffee_drink at the global scope had been changed.  Though this is possible, it is considered bad practice. For very long codes, it may result in errors and make it hard to debug since you ar not sure where the exact change to the global variable had been made if there are many functions who make changes to it.

Good practice for functions will be to read the global variable and return the changed variable upon calling it but not to change the global variable directly.  

eg 5. 

coffee_drink = "long black"

def cuppa():
    return coffee_drink + " latte"
    

print("The new coffee drink is:", cuppa()
print("Drink outside cuppa function:", coffee_drink)

This will return you:

The new coffee drink is: long black latte
Drink outside cuppa function: long black.

And there you have it! Hope this helps to explain to you about local and global scope. 

Comments