Table of Contents
What is scope in python tutorials point :
Python Scope: A variable is only available from inside the region it is created. This is called scope .The concept of scope rules how variables and names are looked up in your code. It determines the visibility of a variable within the code. The scope of a name or variable depends on the place in your code where you create that variable.
The scope of a name defines the area of a program in which we can unambiguously access that name, such as variables, functions, objects, and so on. A name will only be visible to and accessible by the code in its scope. Several programming languages take advantage of scope for avoiding name collisions and unpredictable behaviours.
Scope is the part of the program or the area of code where we can access a python object is accessible.So the accessing has to be allowed by the scope of the python object.It defines the hierarchy level in which we search namespaces for mappings for name to objects.
Local variables are the one that are defined inside the body of the function,when the function ends the availability of the variable also ends.
Global variables are defined outside the function body so they can be accessed by all functions.
When we call a function, the variables declared inside it are brought into scope.
Scope Resolution
Namespace is the unique identification of the variable or we can call it a container for mapping names to objects.
Namespace tells the python interpreter about the name and the location of the object from where it is trying to access it.
The Namespaces are searched for scope resolution according to the LEGB rule. The LEGB stands for where L is Local, E is Enclosed, G is Global, B is Built-in. This means The variable is first searched in Local, followed by Enclosed, then global and finally built-in.
There are 2 Types of Scope:
1) Local scope: The names that you define in this scope are only available or visible to the code within the scope. A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.
Example: A variable created inside a function is available inside that function:
def func_name():
x = 300
print(x)
2) Global scope: The names that we define in this scope are available to all your code. A variable created in the main body of the Python code is a global variable and belongs to the global scope .Global variables are available from within any scope, global and local.
Example: A variable created outside of a function is global and can be used by anyone.
x = 300
def func_name():
print(x)
func_name()
print(x)
Names and Scopes in Python
Since Python is a dynamically-typed language, variables in Python come into existence when you first assign them a value. On the other hand, functions and classes are available after you define them using def or class, respectively. Finally, modules exist after you import them.
We can use operations like Assignment(=), Import operations, function definitions, argument definitions in the context of functions, class definitions to create or , in case of assignment to update new python names because all of them assign a name to a variable, constant , functions, class, instance, module or other python objects.
Use of Scope Resolution in Python tutorials point
Python Scope Resolution : It defines the hierarchical order in which we have to search the namespaces for obtaining the mapping o the name-to-object (variables). Variables exist in this context, and from this, they are reference. It also defines the lifetime and accessibility of a variable. Below is an example.
Below we have listed the scopes in terms of their hierarchy (narrowest t broadest):
• Local (L) – We can define it inside the function class.
• Enclosed (E) – We can define it inside the enclosing function (Nested function).
• Global (G) – We can define it at the uppermost level.
• Built-in (B) – These are the reserve name in the built-in modules of Python.
Summary :
In this article we saw Scope Resolution In Python Tutorials Point so about this article you have any query then free to ask me
Also read :Database in python
Name of Intern who share this Task :
Charul gupta
My name is Charul Gupta have applied for internship via Internshala.Here is my link and copy of the task .link for task-https://github.com/Charul7301/github_example/blob/master/Sach%20Educational%20Support.docx