Python Namespace and Scope
In this article, you will learn the letters; mapping the names of items and the size of the change.
Table of Contents
- What is Name in Python?
- What is a Namespace in Python?
- Python Variable Scope
- Example of Scope and Namespace in Python
What is the name of Python?
If you have ever read the 'Zen Zen' (the Type of This Settlement "Python Interpretation"), the country will run, the namespaces are better than the idea - more than those who do it! So, what was the title of the book? Let's see the name first.
The name (also known as identity) is easily known as the name of the object. Everything in Python is a thing. The name is a method of finding the item.
For example, when I work a = 2, 2 here is stored in the memory of an unwanted name. I can not speak (RAM) of the item through the built-in jobs, id (). Let's see.
# Note: You may get a different value of id
a = 2
# Output: id(2)= 10919424
print('id(2) =', id(2))
# Output: id(a) = 10919424
print('id(a) =', id(a))
Next, both refer to the same thing. Let's make it a little fun.
# Note: You may get the different value of id
a = 2
# Output: id(a) = 10919424
print('id(a) =', id(a))
a = a+1
# Output: id(a) = 10919456
print('id(a) =', id(a))
# Output: id(3) = 10919456
print('id(3) =', id(3))
b = 2
# Output: id(2)= 10919424
print('id(2) =', id(2))
What happened in the previous steps? The diagram will help us to explain this.
All these are valid and refer to three different types of biological diversity. Also the product also, so the name can also refer them.
What is a Namespace in Python?
So we know the name of the thing, we can move the idea of the names.
The article is the name of his name.
In praying, you can estimate namespace such as mapping each name, what would be if something was related.
Different names can be arranged at the end of the day, but most of the time.
A namespace includes all the names that were created when I started an Interpreter Python without too much time.
This is because the complexity of operations, such as identification (), print (), etc. can be found in all parts of the program. Every article creates its own name.
They are open to many different names. Therefore, the same name can be different parts that do not fall.
Designs can have many jobs and classes. Names are created when calling to work, all the names are defined. Similarly, it is the case in the classroom. The following diagram can help you explain this.
Python Variable Scope:
Although there are several namespaces specifically defined, they may not be able to access them all over the program. The concept of coverage is a game.
the designated portion of the program which can be named directly without prior authorization.
At any time, there are at least three quarters.
1. The scope of current activities with names
2. The scope of the module application with a global name
3. The size of the outer space is included in the name
When a reference is made, the domain name, the search engine, then the domain name, and finally the domain name are added.
If there is another job that works, new varieties are in the area.
Example of Scope and
Namespace in Python
def outer_function():
b = 20
def inner_func():
c = 30
a = 10
In this program, the three variables are defined as a specific name that is available. With the program,
def outer_function():
global a
a = 20
def inner_function():
global a
a = 30
print('a =',a)
inner_function()
print('a =',a)
a = 10
outer_function()
print('a =',a)
Output:
a = 30
a = 30
a = 30
In this case, all the reference work of the world because of the use of an important international word.
Comments
Post a Comment