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

  • Cloud Native Python
  • Manish Sethi
  • 200字
  • 2021-07-02 19:11:56

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
主站蜘蛛池模板: 安西县| 新闻| 怀来县| 陵川县| 什邡市| 五河县| 肃南| 台中市| 揭西县| 山西省| 神农架林区| 金乡县| 银川市| 黔南| 迭部县| 山丹县| 阳城县| 边坝县| 洛南县| 观塘区| 公主岭市| 漠河县| 高平市| 吉林市| 无为县| 辉南县| 东乌珠穆沁旗| 资源县| 双鸭山市| 保定市| 青河县| 广东省| 珲春市| 九龙县| 六安市| 维西| 台前县| 莒南县| 洞头县| 长武县| 正镶白旗|