MyAI

Modules

A module is a file containing definition of functions, classes, variables, constants or any other Python object. Contents of this file can be made available to any other program. Python has the import keyword for this purpose.

Types

User Defined Modules

——mymodules.py——-

def myname(name):
    print('My Name', name)
    return

——mytestfile.py——

import mymodules
mymodules.myname('sunil')

Results: MyName sunil

Importing Modules:

Module Attributes

In Python, a module is an object of module class, and hence it is characterized by attributes.

Following are the module attributes −

ex:

import mymodules
print ("__file__ attribute:", mymodules.__file__)
print ("__doc__ attribute:", mymodules.__doc__)
print ("__name__ attribute:", mymodules.__name__)

Result:

__file__ attribute: /home/sunil/Desktop/Sunil/MyAutomation/Python/test.py
__doc__ attribute: None
__name__ attribute: test

Note: If you edit the .py file and save it, the function loaded in the memory won’t update. You need to reload it, using reload() function in imp module.


Main Page   Next Chapter: Python - Strings