Python Scopes and Their Built-in Functions
What you will learn?
In Python, variables are just not accessible from the class they have been declared in. To know where variables exist in a program and how to access them depends on how they have been declared. The part of the program where variables, functions, and objects are easily accessible is commonly referred to as scope in Python.
Types of Scopes in Python:
In Python, there are four types of scopes, which are as follows:
Global Scope (with example)
Global scope refers to the names of variables which are defined in the main body of a program. These are visible and accessed throughout the program. The variables or objects declared in the global scope are easily accessible to all functions within the program. Let’s understand the global scope with the help of a code.
Attention: As a good programming habit, it is advisable to avoid global variables as much as possible. Why? Because they are easy to alter and the result could be erroneous output. Global variables increase the security vulnerability of the code as well. That doesn’t mean you will not use global variables at all. As a thumb rule, try to use those variables and objects in the global scope, which are meant to be explicitly used globally like functions and objects.
Local scope refers to the names which are defined within a function and are local to that function. They can be accessed from the point of its definition until the end of the block in which it has been defined. The local scope exists till the time the function has been executed. Let’s understand the local scope with the help of a code.
Notice the error if you run the following code.
Enclosing scope is also known as non-local scope. They refer to the names of a variable defined in the nested function. Simply put, these variables are neither present in the local scope nor in the global scope. To create a non-local variable in an enclosing scope, use a non-local keyword. Let’s understand the enclosing scope with the help of a code.
When the variable or object is not found in local, global, or enclosing scope, then Python starts looking for it in the built-in scope. Built-in scopes are one of the widest scopes that cover all the reserved keywords. These are easy to call anywhere in the program prior to using them, without the need to define them.
Python scope’s behavior is strict. Though python allows accessibility to global names from anywhere, their modification is highly restricted. With the help of allowed keywords, you can modify the behavior of a Python scope. The two keywords allowed in Python to modify the behavior of a Python scope are:
Global Keyword
To define a variable declared inside a function as global, we have to use the ‘global’ keyword. By using a global keyword followed by a variable name, you are asking Python to use the globally defined variable instead of creating a local variable. Let’s understand this concept with a code snippet.
You are free to use multiple global statements with a name. All the names that you list in a global statement will be automatically mapped to the global scope in which you define them. Let us understand how to use a global keyword with the help of a code.
Nonlocal Keyword
Similar to the global keyword, Python also allows nonlocal names to be accessed within functions. To use the keyword, type nonlocal followed by the variable name. When using more than one variable, use a comma. Let us learn how to use nonlocal keywords with the help of a code.
LEGB is an abbreviation for (Local Enclosing Global Built-in) followed by the Python interpreter when executing a code.
The LEGB rule in Python is a name searching algorithm where Python looks up scopes in a particular order. For instance, if you want to look up a reference name, Python will look after all the scopes following the LEGB rule. That means, the interpreter will look for local scope, then global scope, followed by enclosing tag and then finally looking into built-in scopes. If the name is not present on either of the four scopes, you will perhaps get an error.
Python Built-in functions relate to Python scope and namespace. The most commonly used scopes are globals(), locals(), dirs(), and vars(), to name a few . These functions make it easy to fetch information about a Python scope or namespace. As they are built-in, they are available for free and you do not need to fetch from any library or import from a module.
globals()
The globals() function relates to the scope and namespaces in Python. It updates and returns a dictionary representing the current global symbol table. When you call globals() within a function block, names that can be accessed globally from the function will be returned. Let us understand globals() with the help of a code.
locals()
Another function is locals(), which is related to scope and namespaces in Python. It updates and returns a dictionary with a copy of the current state of the local Python scope. When you call locals() within a function block, names that can be accessed locally from the function will be returned. Let us understand locals() with the help of a code.
Let’s have a look at one more code:
dir()
An important built-in function, dir () relates to scope and namespaces in python. It returns a list of valid attributes. Moreover, dir() function behavior differs with a different type of object, as it targets to generate the most relevant one instead of complete information. Let us understand dir() with the help of a code.
vars()
The vars() function is another in-built function related to scope and namespaces in Python. It returns __dict__ attribute for modules, classes, instances, or objects. It is important to note that the __dict__ is a special dictionary used by Python to implement namespaces. Remember, if you miss writing the vars() attribute, it will merely behave like locals(). Let us understand vars() with the help of a code.
Conclusion:
Python scopes are effective in writing reliable and efficient codes in the most straightforward and tech-savvy way. As the visibility of a name is sophistically defined, the chances of bugs that arise due to name collision or bad use of global names are very low.
Research & References of Python Scopes and Their Built-in Functions|A&C Accounting And Tax Services
Source