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', 

>>> x.__name__

>>> x.__file__

>>> 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

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

Modules and Module Files

 What Is Module

 "import module" - Two-Step Process

 sys.modules - Listing Loaded Modules

 importlib.reload(module) - Reloading Module

What Are Module Members

 "from module import member" Statement

 "from module import *" Statement

 What Is __all__ List

 __pycache__/module.version.pyc Files

 What Is the __main__ Module

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab


 Full Version in PDF/EPUB