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?
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With