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

  • 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
主站蜘蛛池模板: 河津市| 元氏县| 台中县| 马鞍山市| 泰州市| 新河县| 兴仁县| 明光市| 武定县| 全椒县| 景泰县| 桐乡市| 聂荣县| 内丘县| 巴林左旗| 盐源县| 武平县| 浦城县| 济源市| 湘阴县| 石狮市| 伊春市| 连山| 伊金霍洛旗| 锦州市| 淮安市| 如东县| 西丰县| 斗六市| 贵港市| 晋宁县| 灵川县| 武城县| 六安市| 浑源县| 来宾市| 克山县| 文登市| 琼中| 达州市| 阳新县|