Modular----This is what our programmers dream of, and by modularity we can avoid repetitive manufacturing wheels.
While
Modules allow you to logically organize your Python code snippets.
Assigning the relevant code to a module will make your code better and easier to understand.
The module is also a Python object, with a random name attribute used to bind or reference.
Simply put, the module is a file that holds the Python code. Modules can define functions, classes, and variables. The module can also contain executable code.
For example, print output:
Encapsulates the printed data as a module, and each time the module is called, the text that begins with Hello is output
Module code:
Vim demomodule1.py
The contents are as follows:
#!/usr/bin/python
#-*-Coding:utf-8-*-
# define module: Begin:
def sayHello (hello):
Print "Hello,", hello
Return # #结束定义 End
The function of using the module is as follows:
#!/usr/bin/python
#-*-Coding:utf-8-*-
# #导入模块 functions defined in the demode.py
Import DemoModule1
# #call function in demodef.py
Demomodule1.sayhello ("haha")
# # #这种调用的方法是错误的 SayHello ("++++++++1234567++++")
Run and output:
Error Summary:
This is forgotten add:
Python Programming Tips 2