Modules Are Objects Too

Q

Are modules objects in Python?

✍: FYIcenter.com

A

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

 

Defining and Using Python Code Modules

⇒⇒Python Tutorials

2017-09-09, 111👍, 0💬