wow yoo wow yoo - 1 month ago 8
Python Question

Do modules have types in Python?

I'm reading PEP338 .Some words confused me:


If the module is found, and is of type PY_SOURCE or PY_COMPILED , then the command line is effectively reinterpreted from
python <options> -m <module> <args>
to
python <options> <filename> <args>
.


Do modules have types in Python?

Answer

Modules can be loaded from different sources. The author refers to 2 specific sources the module was loaded from, see the imp module documentation:

imp.PY_SOURCE
The module was found as a source file.

[...]

imp.PY_COMPILED
The module was found as a compiled code object file.

[...]

imp.C_EXTENSION
The module was found as dynamically loadable shared library.

These values are used in the return value of the imp.get_suffixes() function, among others.

The PEP states that only modules loaded from source (.py files) and from a bytecode cache file (.pyc) are supported; the -m switch does not support C extension modules (typically .so or .dll dynamically loaded libraries).

The resulting module object is still just a module object; the word type in the text you found is not referring Python's type system.

Comments