Python Scope

The purpose of this article is to provide readers with a brief overview of Python scope, including examples to satisfy their need for knowledge.

Python variables store data values. A variable is a reference to an object in memory, which means it gets mapped to an instance whenever it is assigned to an instance.

It differs from other languages such as C, C++, and JAVA in the fact that Python is not “typed statically ”.

In Python scope, a variable is only accessible within the region where it was created – It’s not necessary to declare variables or define their types before we operate them. This is known as Python scope.



What is Python Scope ?

Python scope defines the visibility and accessibility of variables, functions, and objects in a program. It determines where a variable or name can be accessed.

There are four types of scopes in Python:

  1. Local scope: Variables defined within a function are only accessible within that function. Once the function completes, these variables are destroyed and cannot be accessed.
  2. Enclosing scope: Also known as non-local scope, it applies to variables in nested functions. Inner functions can access and modify variables from their own scope, as well as from the enclosing and global scopes.
  3. Global scope: Variables defined outside of any function or class have global scope. They can be accessed and modified from anywhere in the program, including within functions. Global variables persist throughout the program’s execution.
  4. Built-in scope: Built-in scope includes pre-defined names and functions provided by Python, such as print(), len(), and range(). These names are accessible from any part of the program without requiring explicit import statements.

Function Inside Function

In the example above, the variable mrx is not accessible outside the function, but within the function in Python scope, it is accessible.

A function within the function can access the local variable:

Example: 

def anyNum(): mrx = 88 def anyNum_innerfunc(): print(mrx) anyNum_innerfunc() anyNum()

You can access a local variable within a function using the following syntax:

Example: 

def anyString(): mrx = "Apple" def anyString_innerfunc(): print(mrx) anyString_innerfunc() anyString()

Local Scope

When it comes to Python scope, a variable declared inside a function belongs to its local scope, and can only be accessed inside it.

Assigning a variable within a function makes it accessible inside that function:

Example: 

def anyNum(): mrx = 300 print(mrx) anyNum()

A function’s internal variables are accessible inside that function:

Example: 

def anyString(): mrx = "Apple" print(mrx) anyString()

Global Scope

Global variables are those that are formed in the main body of Python code and are part of the global scope.

Any scope, both global and local, has access to global variables.

A global variable is one that is declared outside of a function and is accessible to everyone:

Example: 

mrx = 45 def anyNum(): print(mrx) anyNum() print(mrx)

Everybody can access a global variable because it was declared outside of a function:

Example: 

mrx = "Apple" def anyString(): print(mrx) anyString() print(mrx)

Global Keyword

The global keyword can be utilized if you have to specify a global variable but are restricted to the local scope.

The variable is made global with the global keyword.

The variable is part of the global scope if you use the global keyword:

Example: 

def anyNum(): global mrx mrx = 88anyNum()print(mrx)

If you use the global keyword, the variable is a part of the global scope:

Example: 

def anyString(): global mrx mrx = "Apple"anyString()print(mrx)

Additionally, if you need to modify a global variable inside of a function, utilize the global keyword.

When referencing a global variable inside of a function, use the global keyword to alter the value of the variable:

Example: 

x = 54 def anyNum(): global mrx mrx = 88 anyNum() print(mrx)

Use the global keyword to change a global variable’s value when referencing it inside of a function:

Example: 

x = "Samsung" def anyString(): global mrx mrx = "Apple" anyString() print(mrx)

Naming Variables

Python will interpret two variables with the same name used inside and outside of a function as two independent variables, one available in the global scope (outside the function) and one available in the local scope (inside the function), if you operate on them both.

The code then displays the global mrx after the function displays the local mrx:

Example: 

mrx = 88 def anyNum(): mrx = 54 print(mrx) anyNum() print(mrx)

After the function has shown the local mrx, the code then shows the global mrx:

Example: 

mrx = "Apple" def anyString(): mrx = "Samsung" print(mrx) anyString() print(mrx)

Python Scope Importance

Python scope is important for several reasons:

  1. Understanding scope is crucial for determining where variables can be accessed and used within a program. By defining variables in the appropriate scope, you can ensure that they are accessible only where needed and avoid naming conflicts.
  2. Scoping allows you to encapsulate variables within specific parts of your code. Local variables are limited to their respective functions, providing data privacy and preventing unintended modifications or access from other parts of the program.
  3. Proper scoping helps in organizing and structuring your code. By defining variables in the appropriate scope, you can group related variables together, making the code more readable and easier to understand.
  4. Scoping prevents naming conflicts by limiting the visibility of variables. By using local variables within functions and avoiding the use of global variables unless necessary, you can minimize the chances of naming collisions and make the code more robust.
  5. By properly scoping variables, you can create modular and reusable code. Encapsulation of variables within functions allows for code reuse without worrying about variable clashes or unintended side effects.
Subscribe to our Newsletter and stay up to date with the latest technical news and information.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *