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

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
主站蜘蛛池模板: 陈巴尔虎旗| 肃南| 迭部县| 秦皇岛市| 平潭县| 大洼县| 如皋市| 镇赉县| 新河县| 科技| 韩城市| 呼图壁县| 绿春县| 如东县| 韶山市| 新泰市| 伊川县| 平罗县| 汉寿县| 西乡县| 湖口县| 米脂县| 工布江达县| 宁德市| 科尔| 沾化县| 化州市| 三原县| 沧源| 吴江市| 漠河县| 武山县| 卫辉市| 镶黄旗| 锡林浩特市| 厦门市| 安平县| 双鸭山市| 武城县| 漳州市| 江津市|