- 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
推薦閱讀
- Mastering OpenLayers 3
- 玩轉(zhuǎn)Scratch少兒趣味編程
- 深度實踐OpenStack:基于Python的OpenStack組件開發(fā)
- Learning ArcGIS Pro 2
- CKA/CKAD應(yīng)試教程:從Docker到Kubernetes完全攻略
- 算法訓(xùn)練營:提高篇(全彩版)
- RISC-V體系結(jié)構(gòu)編程與實踐(第2版)
- Python編程從0到1(視頻教學(xué)版)
- 從零開始學(xué)C語言
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- Advanced Express Web Application Development
- 計算機應(yīng)用基礎(chǔ)案例教程
- 硬件產(chǎn)品設(shè)計與開發(fā):從原型到交付
- 嵌入式Linux C語言程序設(shè)計基礎(chǔ)教程
- 企業(yè)級Java現(xiàn)代化:寫給開發(fā)者的云原生簡明指南