Journey to Master Python (WIP)

ageekymonk
2,809 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Namespace

In python there are three different namespace

  1. global
  2. local
  3. builtin

Rougly namespace in python in a dictionary containing bunch of variables. So every object has __dict__ associated with it. That contains all the attributes that belong to that object. You can modify them for fun and profit.

Tricky bit is the scope of the namespace and where are those visible. This is defined by the scope. Variable is first searched in the inner most namespace, and then to enclosing namespace (for example functions inside functions) and moves to global and finally builtin.

Global Namespace

To see what to see in modules' namespace, you need to access its __dict__ which is done by calling globals(). If you are running a standalone python file then the module is called __main__

module_dict = globals()
Global Namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# To get module dictionary.
module_dict = globals()
# Adding a variable to the module namespace
my_module_var = 'hello'
print(f'Module name: {module_dict["__name__"]}, Value of my my_module_var: {module_dict["my_module_var"]}')
# Adding a variable to module namespace by adding to the dictionary directly
module_dict["my_new_var"] = "world"
# Accessing created variable
print(f'Value of my_new_var is {my_new_var}')
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Builtin Namespace

Python automatically imports all the builtin functions into every modules' namespace without polluting the modules' __dict__. So when you see modules' dictionary you wont see them directly. You can still access them in variety of ways.

In below example we are modifying builtin function.

Builtin Namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Accessing builtin namespace via module's namespace
module_dict = globals()
builtin_dict = module_dict["__builtins__"].__dict__
# you can also access by importing builtins
import builtins as _builtins
# Both of them are same
print(builtin_dict == vars(_builtins))
# Modifying the builtin print function
_print = _builtins.print
_builtins.print = lambda *args, **kwargs: _print('YodaSays: ', *args, **kwargs)
print("Do. Or do not. There is no try")
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

vars() returns the local namespace. If it is called inside function it returns functions __dict__ It also takes an argument which could be any object and returns its __dict__ object.

Local Namespace

This could be module or function or class. To get access to local namespace dict you can call locals() or if you want to access any object's namespace call vars(objname).

Inside function if you call locals() or vars() you will get currently visible namespace as dictionary and should not be modified.

But inside class/module you get the object's __dict__. You can modify for fun and profit.

Local Namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Here the current namespace is module. So local is equal to global
print(locals() == globals())
def yoda_says(*args, **kwargs):
prefix = "YodaSays :"
print(locals())
quote_list = ["Always pass on what you have learned"]
# you wont see quote_list as it is not visible at that point for locals while it is constructing it.
yoda_says()
class Yoda:
quote_list = ["Always pass on what you have learned"]
yoda_dict = locals()
yoda_dict['movies'] = ['The Phantom Menace', 'Attack of the clones', 'Revenge of the sith']
print(vars(Yoda))
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content