- 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
推薦閱讀
- 多媒體CAI課件設計與制作導論(第二版)
- Learning Neo4j
- Software Testing using Visual Studio 2012
- 數(shù)據(jù)結(jié)構(gòu)簡明教程(第2版)微課版
- Django:Web Development with Python
- 區(qū)塊鏈:以太坊DApp開發(fā)實戰(zhàn)
- Python應用輕松入門
- MySQL數(shù)據(jù)庫管理與開發(fā)(慕課版)
- Mastering Ext JS
- ASP.NET程序設計教程
- Extreme C
- Python計算機視覺和自然語言處理
- Android 游戲開發(fā)大全(第二版)
- Visual Basic程序設計基礎
- 企業(yè)級Java現(xiàn)代化:寫給開發(fā)者的云原生簡明指南