logo
logo
Sign in

Namespaces and scope in Python

avatar
skill technical
Namespaces and scope in Python

 

In these articles, we will discuss namespaces, scopes and its types.

 

Namespaces

Numerous programming languages use namespaces or surrounds for identifiers. An identifier defined in a namespace is associated with that namespace. This way, the same identifier can be singly defined in multiple namespaces (Like the same filename in different directories). Programming languages, which support namespaces, may have different rules that determine to which namespace an identifier belongs to.

 

Namespaces in Python are enforced as Python wordbooks, that is, they're defined by a mapping of names, i.e. the keys of the wordbook, to objects, i.e. the values. The stoner does not have to know this to write a Python program and when using namespaces.

 

Types of Namespaces

  • Local Namespace:
  • Global Namespace
  • Built-in Namespace:

 

 

Scope

 

Namespaces help us uniquely identify all the names inside a program. Still, this does not indicate that we can use a variable name anywhere we want. A name also has a compass that defines the corridor of the program where you could use that name without using any prefix. Just like namespaces, there are also multiple reaches in a program. Then's a list of some reaches that can live during the prosecution of a program.

Local scope, which is the inmost scope that contains a list of original names available in the current function.

A scope of all the enclosing functions. The hunt for a name starts from the nearest enclosing scope and moves outwards.

A module position compass that contains all the global names from the current module.

The remotest compass that contains a list of all the erected-in names. This compass is searched last to find the name that you substantiated.

 

Types of Scopes in Python

 

In Python, there are four types of reaches, which are as follows.

Global scope




collect
0
avatar
skill technical
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more