Modules Are Objects Too


Are modules objects in Python?



Yes, all modules are objects of "module" type in Python? You can verify this with the following Python code:

>>> import firstModule
>>> type(firstModule)
<class 'module'>

In other words, the "import firstModule" statement performed two activities:

  • Creates a new "module" object and hiding its Python code inside.
  • Introduced a variable "firstModule" to reference to the "module" object.

Like any other types of objects, you can use the "dir()" to list its members:

>>> import firstModule
>>> dir(firstModule)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', 
'Python Tutorials', '__package__', '__spec__', '1.00', 'firstClass', 


What Is Module Package

'import' Module Loading Statement

Defining and Using Python Code Modules

⇑⇑ Python Tutorials

2022-08-26, 1171🔥, 0💬