Display module content

The module content is usually displayed using the dir() function, which displays the attributes provided by the module. Here is an example:

>>> import simple_module
>>> dir(simple_module)
['Bye', 'Welcome', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
>>>

In the code above we have imported the module named simple_module. We’ve then used the dir () function to display the attributes provided by the module. Here is a brief description of the attributes:

  • Bye, Welcome – the functions provided by the module
  • __builtins__ – a listing of all the built-in attributes that are accessible from the module.
  • __cached__ – the name and location of the cached file associated with the module.
  • __doc__ – help information for the module.
  • __file__ – the name and location of the module, relative to the current Python directory.
  • __loader__ – the loader information for this module.
  • __name__ – the name of the module.
  • __package__ – used internally by the import system.
  • __spec__ – set to the module spec that was used when importing the module.

You can get even more information about each attribute. For example, to display more information about the Bye function, we can use the following command:

>>> dir(simple_module.Bye)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>>
Geek University 2022