Before introducing the concept of scope in python , I would like to talk about the term namespace as both are closely related to each other.
Namespace – It is an object that holds a set of names . In python , the object is dictionary . You can access the names by listing the keys of dictionary. When you create a new entity it is stored in the namespace depending on where you create the entity in the program.
Namespaces support modular programming by preventing naming conflicts.
Example –
>>> import os
>>> os.__dict__.keys()
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__file__', '__cached__', '__builtins__', 'abc', 'sys', 'st', '__all__', '_exists', '_get_exports_list', 'name', 'linesep', 'stat', 'access', 'chdir', 'chmod', 'getcwd', 'getcwdb', 'link', 'listdir', 'lstat', 'mkdir', 'readlink', 'rename', 'replace', 'rmdir', 'symlink', 'system', 'umask' .....truncate .... ])
>>>
In the above code, the list of all names is the namespace of os module.