Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is definition order available in a module namespace?

It's documented that the definition order in classes is preserved (see also PEP 520):

If the metaclass has no __prepare__ attribute, then the class namespace is initialised as an empty ordered mapping.

Is the definition order also preserved in module objects?

# foo_bar.py

def foo():
    pass

def bar():
    pass

I've experimented with the module above (also swapping the ordering), and it did seem to be reliable:

>>> import foo_bar
>>> for name in foo_bar.__dict__:
...     if not name.startswith('_'):
...         print(name)
... 
foo
bar

Presumably, the module namespace also uses a compact dict underneath, or perhaps it follows from the fact that the type(foo_bar) is a <class 'module'> that it must also respect definition order, like any other class. However, I'm not sure if this is a feature guaranteed by Python, or just a CPython implementation detail. Are the names in modules required to respect definition ordering?

like image 950
wim Avatar asked Mar 15 '17 00:03

wim


People also ask

Is a module a namespace?

A module is a way which is used to organize the code in separate files and can execute in their local scope, not in the global scope. A namespace is a way which is used for logical grouping of functionalities with local scoping.

How do you know all the names defined in a module?

Method 1: Using the dir() Function: We have first to import the module in the Python shell, and then we have to write the module name in the dir() method, and it will return the list of all functions present in a particular Python module.

What is user defined modules in Python?

user-defined modules: Python files which are imported in to the top-level file, or among each other, and provide separate functionalities. These files are usually not launched directly from your command prompt, and are custom-made for the purpose of the project.

Can two different packages have modules with same name?

This is not possible with the pip. All of the packages on PyPI have unique names. Packages often require and depend on each other, and assume the name will not change. Even if you manage to put the code on Python path, when importing a module, python searches the paths in sys.


1 Answers

Built-in classes, like the module class, don't go through the normal mechanism user-defined classes do* and, as such, don't make use of metaclass.__prepare__. PEP 520 does not apply for them so the guarantees it extends cannot be applied here.

The ordering of the module namespace is currently preserved due to the dictionary being insertion ordered so, like the dictionary itself, is considered an implementation detail.


* User defined classes first go through build_class (the function the LOAD_BUILD_CLASS bytecode loads when you dis a class statement) in bltinmodule.c. This is the only place where __prepare__ is invoked (which returns a PyDict_New from type_prepare if a custom meta with a __prepare__ isn't defined).

like image 196
Dimitris Fasarakis Hilliard Avatar answered Sep 20 '22 01:09

Dimitris Fasarakis Hilliard