Home >Backend Development >Python Tutorial >Namespaces and scopes in Python
In Python, every package, module, class, function, and method function has a "namespace" in which variable names are resolved. The following article will introduce you to the namespace and scope in Python. I hope it will be helpful to you.
What is a namespace:
A namespace is a system used to ensure that a program All names are unique and can be used without any conflicts. Multiple namespaces can use the same name and map it to different objects. [Video tutorial recommendation: Python tutorial]
For example, the directory file system structure in the computer. Needless to say, one can have multiple directories, and each directory can have a file with the same name. However, one can direct a file by specifying its absolute path.
Namespaces in Python are implemented as Python dictionaries, which means it is a mapping from names (keys) to objects (values). Users do not need to know this to write Python programs and when using namespaces.
Types of namespaces:
Local namespace: This namespace contains local names within functions. This namespace is created when a function is called, and it only lasts until the function returns.
Global namespace: This namespace contains the names of various imported modules you use in your project. It is created when the module is included in the project and lasts until the end of the script.
Built-in namespace: This namespace contains built-in function and built-in exception names.
The life cycle of the namespace:
The life cycle of the namespace depends on the scope of the object. If the scope of the object ends, The life cycle of the namespace ends. Therefore, objects of the inner namespace cannot be accessed from the outer namespace.
What is scope?
Namespace can help us uniquely identify all names in the program. However, this does not mean that we can use variable names everywhere. The name also has a scope that defines the parts of the program that can use the name without any prefix. Just like namespaces, there are multiple scopes in a program.
Scope refers to an area of the program in which the namespace can be accessed directly, that is, without using a namespace prefix. In other words: the scope of a name is an area of the program where the name can be used explicitly, for example inside a function.
The namespace of a name is the same as its scope. Scopes are statically defined, but they are used dynamically.
The following is a list of some scopes that may exist during program execution:
● Local scope: It is the innermost scope and contains the list of local names available in the current function.
● Scope of all enclosing functions: Search names start from the nearest enclosing scope and move outward.
● Module-level scope that contains all global names in the current module.
● The outermost scope that contains all built-in name lists. Finally search this range for the name you referenced.
The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !
The above is the detailed content of Namespaces and scopes in Python. For more information, please follow other related articles on the PHP Chinese website!