Does Python have a Block Scope? (Scope in Python explained)

Does Python have a Block Scope?

Python doesn’t have a block scope. If you come from a programming languages background like C, C++, or Java, In python there is no such thing called block scope.

What is block scope?

If a variable is declared inside an intended block of code like For loop, While loop, or condition flow (if else) The variable can only be accessed within a block

Python does not have a Block Scope check try the below examples

In Condition flow:

to_continue = True
if to_continue:
	variable = "block scope"
print(variable)

Output:

block scope

In While Loop:

while to_continue:
    variable = "block scope"
    print("Loop")
    to_continue=False
print(variable)

Output:

Loop
block scope

In For Loop:

for _ in range(5):
    variable = "block scope"
    print("Loop")
print(variable)

Try it yourself

Output:

Loop
Loop
Loop
Loop
Loop
block scope

As we could see there is no block scope in Python, Variable can be accessed outside of the block except functions (Which is explained below)

Scope in Python

It’s essential to understand the scope of variables. It refers to the extent to which the variable is visible or accessible by the Python code.

In Python, The scope is determined by its indentation level. For example, A block of code within a function or a loop can only be accessible within that block and it will not be accessible outside of the block

Indentation in Python

Indentation is to add spaces at the beginning of a line of code to make sure that it is part of a block of code, especially In Python, indentation is essential and is used to define blocks of code such as loops, conditions, and functions

For example:

if x > 5: 
    print("x is greater than 5") 
    x = x + 1 
    print("x has been incremented")

There are 2 main types of scope in Python and it decides how to enable or restrict the use of variables across the program.

  • Global Scope
  • Local Scope

Global Scope:

A variable declared in the program Will be made accessible across all parts of the program like within Loop, Condition Scope, functions, etc

global_variable = "testing"

def my_function():
  print ("Inside Function -> " + global_variable)

my_function()
print("Outside Function -> " + global_variable)

Try it Yourself

Output:

Inside Function -> testing
Outside Function -> testing

The above example shows the global variable can be accessed from anywhere in the program

Local Scope:

A variable declared inside a function will be restricted inside the function and can’t be accessible outside of it.

def my_function():
  global_variable = "testing"
  print ("Inside Function -> " + global_variable)

my_function()

Try it Yourself

Output:

Inside Function -> testing

In the above example, We could see the variable got declared inside the function will be accessed inside the function

def my_function():
  global_variable = "testing"
 
my_function()
print("Outside Function -> " + global_variable)

Try it Yourself

Output:

Traceback (most recent call last):
  File "main.py", line 38, in <module>
    print("Outside Function -> " + global_variable)
NameError: name 'global_variable' is not defined

Try accessing the variable declared inside the function will result in “NameError” as above.

"NameError: name 'global_variable' is not defined"

The above Error means the variable “global_variable” is unknown for this part of the program.

Hope the above blog helps to understand more about Scope & block scope in Python

Good Luck with your Learning

Similar Posts