top of page
Search

Namespaces and Types in Python

  • Writer: Minu k
    Minu k
  • Jun 15, 2022
  • 2 min read






Namespaces



In this scenario, your capacity to access a specific name is determined by where that name is defined.



A namespace is a set of currently declared symbolic names that also includes information about the item that each name refers to. A namespace can be compared to a dictionary, with the keys being the object names and the values being the objects themselves. Each key-value pair corresponds to a specific object.



The LEGB rule is used to scan the Namespaces for scope resolution. L stands for local, E for enclosed, G for global, and B for built-in. The LEGB sequence is crucial. Local is the first place to look for the variable, then Enclosed, Global, and finally Built-in.



1. Namespace built-in

This namespace contains the names of Python's built-in functions and exceptions.



2. Global Namespace


This namespace contains the names of the imported modules in the project. It is formed when the module is included and lasts till the script is finished.




3. Local Namespace


The function's local names are stored in a local namespace. When the function is invoked, this namespace is created, and the scope expires when the value is returned.


Scope in Python


Scope arose as a result of early programming languages (such as BASIC) having only global names. With a name like this, any component of the programme might change any variable at any time, making maintaining and debugging big applications a tremendous pain. To deal with global names, you'd have to remember all of the code at once in order to know what the value of a given name is at any given time. This was a significant drawback of not having scopes.


To overcome this difficulty, certain languages, such as Python, use scope. When you use a language that implements scope, you won't be able to access all of the variables in a programme from all of the program's places.


Conclusion

In this blog, we discussed about namespaces in python , types of namespaces and scope in python. You can also visit scope of variable in python to learn more about it.

 
 
 

Comments


learnskill123

©2022 by learnskill123. Proudly created with Wix.com

bottom of page