官术网_书友最值得收藏!

Modules

A module basically allows you to logically organize your programming code. It is similar to any other Python program. They are needed in scenarios where we need only a bit of code to be imported instead of the entire program. A module can be a combination of one or multiple functions classes, and many more. We will use a couple of inbuilt functions, which are a part of the Python library. Also, wherever needed, we will create our own modules.

The following example code showcases the structure of modules:

    #myprogram.py 
    ### EXAMPLE PYTHON MODULE
# Define some variables:
numberone = 1
age = 78

# define some functions
def printhello():
print "hello"

def timesfour(input):
print input * 4

# define a class
class house:
def __init__(self):
self.type = raw_input("What type of house? ")
self.height = raw_input("What height (in feet)? ")
self.price = raw_input("How much did it cost? ")
self.age = raw_input("How old is it (in years)? ")

def print_details(self):
print "This house is a/an " + self.height + " foot",
print self.type, "house, " + self.age, "years old and costing\
" + self.price + " dollars."

You can import the preceding module using the following command:

# import myprogram
主站蜘蛛池模板: 缙云县| 翁源县| 呼和浩特市| 陇南市| 榆树市| 福海县| 娄烦县| 南陵县| 祥云县| 肇庆市| 大连市| 公主岭市| 博兴县| 克山县| 广水市| 贡山| 扎赉特旗| 扶风县| 手机| 柏乡县| 宝清县| 元谋县| 绥江县| 济宁市| 丹棱县| 奈曼旗| 临高县| 汉寿县| 日照市| 泸水县| 祥云县| 绵阳市| 衡南县| 固镇县| 华安县| 和政县| 卢龙县| 台北市| 满洲里市| 金昌市| 临潭县|