Disclaimer: This is a user generated content submitted by a member of the WriteUpCafe Community. The views and writings here reflect that of the author and not of WriteUpCafe. If you have any complaints regarding this post kindly report it to us.

Namespace in Python

Python namespace is a collection of names that has a unique name for each and every object in python. The python interpreter understands what exact method or variable, depending upon the namespace. Its Name which means unique identifier, method or variable and Space depends upon the location from where access variable or a method. Namespace in the form of python dictionary, in which keys are the objects names and values are the objects. The python interpreter namespace containing all the built-in names is created as long as interpreter runs.

Types of Namespace

The Built-in functions like id(), print() are always available to us from part of the program. The same name that exists in different modules does not coincide. The local namespace includes local names inside a function and namespace is created when the function is called, and it only lasts the function returns. The global namespace includes names from various imported modules and created when the module is included in the project, and the script ends. The built-in namespace includes built-in functions and built-in exception names.

Python Variable Scope

Python variables are containers but reserved memory locations for storing data values. Python namespace in variable scope is a part of a program, if we access the python namespace without having to use a prefix. The scope of a name defines the area of a program in which we can access that name, such as variables, functions, objects. A name will only be visible and accessible by the code in its scope.

Navigating Namespaces and Scope

Using the LEGB Rule for Python Variable Scope

Local or function scope: The code block or body of a python function or lambda expression. Python variable scope contains names that you define inside the function.

Enclosing or non-local scope: It is a special scope that only exists for nested functions and the local scope is an inner or nested function, and then the enclosing scope is the outer or enclosing function. The enclosing scope is visible from the inner code and enclosing functions.

Global or module scope: It is the top-most scope in a python program, script, or module. The Global scope contains all the names that you define at the top level of a program or a module.

Built-in scope: It is a special python scope that created or loaded whenever you run a script or open an interactive session. It automatically loaded by python when you run program or script.

In Python, scope is implemented as Local, Enclosing, Global, or Built-in scope. When we use variable or name, python searches these scopes sequentially to resolve it. To gather more knowledge about python, you can opt to choose Python training in Kochi. Learning python suggests that you have advanced knowledge in python and skills in the programming field. Python developer training helps you to become a good Python developer and know the basics in the python programming that grab a successful career in python.

Login

Welcome to WriteUpCafe Community

Join our community to engage with fellow bloggers and increase the visibility of your blog.
Join WriteUpCafe