Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
What Are Module Members
This section provides a quick introduction module members, which refer to variables (or attributes), functions and classes defined in a given module. 'dir(module)' function can be used to list all members in a given module.
What Are Module Members? Module members refer to variables (or attributes), functions and classes defined in a given module.
Module members are also considered as names defined a given module. You can use the dir(module) built-in function to list all member in a given module.
Like instance members (attributes and methods), module members can be accessed using the attribute reference expression (module.member). This is expected, because a loaded module is also an instance of the "module" class.
The following code example shows you how to insert a new attribute into a module, and how to list all module members.
>>> import module_test as x Hi there! - from 'module_test' module Welcome on board! - from 'first' class # add a new module attribute x.author = 'Herong' # list all module members >>> dir(x) ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'first', 'help', 'version', 'author'] >>> x.__name__ 'module_test' >>> x.__file__ '/home/herong/module_test.py' >>> x.__loader__ <_frozen_importlib_external.SourceFileLoader object at 0x10a203250>
As you can see from the output, the Python system has added a bunch of attributes in the loaded module.
Table of Contents