How to write a python module?

PythonServer Side ProgrammingProgramming

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py

 create helloworld.py then write the following function as its content:

 def hello_world():
    print "Hello world"

 Now create another file in the same directory called script.py. Write the following as its contents:

 import helloworld
helloworld.hello_world()

 We created the helloworld module and imported it in script.py. Then we used the function from helloworld.py. Creating many modules in the working directory can create a lot of clutter. You can use packages to neatly group multiple modules together. You can read about how packages are created and used in detail here: https://docs.python.org/2/tutorial/modules.html#packages

raja
Published on 19-Dec-2017 14:00:59
Advertisements