Understanding about namespaces, attributes and scopes in Python


I assume you don’t understand about namespaces, attributes and scopes in Python. Because I will explain about this three things that mostly missed by people who start learning with Python. Actually, this things will help us in the future about understanding Python even for advanced programmer. So, let begin with namespaces.

Namespaces according to Wikipedia it’s meaning for :

A namespace (sometimes also called a name scope) is an abstract container or environment created to hold a logical grouping of unique identifiers or symbols (i.e., names). An identifier defined in a namespace is associated only with that namespace. The same identifier can be independently defined in multiple namespaces. That is, the meaning associated with an identifier defined in one namespace may or may not have the same meaning as the same identifier defined in another namespace. Languages that support namespaces specify the rules that determine to which namespace an identifier (not its definition) belongs.[1]

Confusing with this definition ? Yeah, so do I :).

I will start introducing namespaces in Python language. Imagine, Namespaces is like a house. There are 2 house here. On outside, there are two chairs with same brand called “Jati”. Some people want you to move 2 chairs into first house & second house. You can easily identified this same brand name chairs to be : Chair for first home and chair for second home.

Implementation Namespaces in Python?
Let’s see you have function show_value() in file “myblog.py” :

1
2
3
def show_value():
    a = 1
    return a

Then you have same name function in file “mylib.py” :

1
2
3
4
def show_value():
    website = [‘basicpython.com’]

    return website

See, both of files have same name function. So, if the programming language support for namespaces, then should no problem when executing “mylib.py” or “myblog.py”. Even both of function have same code or not.

In Python, namespaces defined as module and it can be individually or hierarchically. If I import “myblog.py” and “mylib.py”, I still can use both of same name function without any problem.

1
2
3
4
5
import myblog
import mylib

print(myblog.show_value())
print(mylib.show_value())

It will show results :

1
2
1
[‘basicpython’]

See, there no problem using same name in Python because it support with Namespaces and can be work hierarchically.

Another thing to know is, how or when namespaces created. Namespaces is created in different moments and different lifetimes. For namespaces that containing built-in names will created when python interpreter start and it’s never deleted. Namespaces by global module is created when it read-in (imported) and deleted after python interpreter quit.

Also, the local namespaces for function is created when function executed / called and stop when it deleted after function give return or raise exception that not handled by function.

Attribute in Python
It for defining any name following with dot. For instance, “myblog.show_value()” which it’s meaning myblog is a module and show_value() is attribute. So, remember, any name following with dot called “attribute”. Attribute can be read-only or writeable. Also, it can be deleted. For example :

1
2
3
4
5
6
7
8
#read-only
a = myblog.show_value()

#writeable
myblog.get_status = 2

#delete
del myblog.get_status

Scopes
Back again into Namespaces. The meaning “Scopes” here is a region that namespaces directly accessible. This scopes is needed to check namespaces when start executing. At least it do three of scope. This are scope ordering by which will running first :
1. Inner, it will searching local names.
2. The scopes of any enclosing function ( non-local and non-global names )
3. Current modules global names
4. Namespace for built-in names

That’s all about Namespaces, attributes and scopes. At least you know part of the Python workflow


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.