Description
function can hide the implementation details of the code, and the module can be placed in a PY file a number of functions, through the module import, you can call these functions, of course, a function as a py file, either way, the py file can be called a module. More generally, the Py file can be used as a module and imported through import.
The modules mentioned here should be modules similar to SYS or OS, rather than regular py files. You can put multiple Python packages in a directory and use the __init__ initialization method to make a Python package that can be used after import is imported.
So here's what needs to be mentioned:
1. How to make and debug the Module 2. Production of the package 3. How to use the common module
1. How to make and debug the module
(1) Production of modules
• The module is made as follows:
#!/usr/bin/env pythonprint ' This module was made by Xpleaf. ' def sayhi ():p rint "Hello, I ' m xpleaf" def work ":p rint" Your work was%s "% Workdef Salary (money):p rint ' Your Salary are %s '% Money
• If executed directly, only non-function parts are displayed:
[Email protected]:/mnt/hgfs/python/day3$ Python my_module1.pythis module is made by Xpleaf.
(2) Module debugging method
-Debugging in the interactive device
• The module can be imported into the interface, using the function functions above:
>>> import tab>>> import my_module1 ===> Import module does not add. py suffix this module is made by xpleaf. ===> non-function part will be executed directly >>> my_module1. ===> Press the TAB key to display the method my_module1 that can be performed under the module. Salary ( my_module1.__name__my_module1. Work ( my_module1.__new__ (my _module1.__class__ ( my_module1.__package__my_module1.__ Delattr__ ( my_module1.__reduce__ (my_module1.__dict__ my_module1.__reduce_ex__ (my_module1.__doc__           MY_MODULE1.__REPR__ (my_module1.__file__ &nbsP; my_module1.__setattr__ (my_module1.__format__ ( my_ Module1.__sizeof__ (my_module1.__getattribute__ ( my_module1.__str__ (my_module1.__hash__ ( my_module1.__subclasshook__ (my_module1.__init__ ( my_module1.sayhi (>>> my_module1.sayhi () ===> function Hello, i ' m xpleaf>>> my_module1 in the execution module. Work (' CEO ') your work is ceo>>> my_module1. Work (' Student ') your work is student>>> my_module1. Salary (' None ') your salary is none
• You can also import only one of the functions in the module:
>>> from My_module1 import sayhithis module was made by Xpleaf.>>> Sayhi () Hello, I ' m xpleaf
-Debug directly in bash
• Each time you have to enter the interaction and import debugging is obviously a bit of trouble;
• You can add the name method to the module file to debug directly in bash;
• The above code is modified as follows:
#!/usr/bin/env pythonprint ' This module was made by Xpleaf. ' def sayhi ():p rint "Hello, I ' m xpleaf" def work ":p rint" Your work was%s "% Workdef Salary (money):p rint ' Your Salary are %s '% moneyif __name__ = = ' __main__ ': #判断被主动执行还被import导入调用sayHi () #主动执行时, the contents after the name statement block will be executed (' Student ') #import导入调用时, the content after the name statement block will not be executed salary (' None ')
• Execution results are as follows:
A. Execute directly in bash [email protected]:/mnt/hgfs/python/day3$ Python my_module1.pythis module is made by Xpleaf. Hello, I ' m xpleafyour work are studentyour salary is None
• Executing in the interaction is the same as the first debugging method;
2. The production of the package
• Consider using packages when packaging multiple module files together;
There are several modules under the my_packets directory:
[email protected]:/mnt/hgfs/python/day3$ ls my_packets/caculate.py my_module1.py
The my_dule1.py content is the same as above, the caculate.py code is as follows:
#!/usr/bin/env Pythonprint ' This modules can and do some caculates. ' def add (num1,num2):p rint '%s +%s is:%s '% (NUM1,NUM2,NUM1+NUM2) def cut (num1,num2):p rint '%s-%s is:%s '% (num1,num2,num1 -NUM2) def multiply (num1,num2):p rint '%s *%s is:%s '% (num1,num2,num1*num2) def div (num1,num2):p rint '%s/%s is:%s '% (num 1,NUM2,NUM1/NUM2)
• To import the My_packets package by importing, you need to create a __init__.py file in that directory and add the appropriate content, as shown below:
[Email protected]:/mnt/hgfs/python/day3$ Touch My_packets/__init__.py[email protected]:/mnt/hgfs/python/day3$ vim my _packets/__init__.py__all__ = [' My_module1 ', ' caculate '] #使用该包下的模块, need to be added here, otherwise not available
• The following is demonstrated in the interactive device:
>>> import tab>>> from my_packets import * ===> Import all Module this module is made by xpleaf under the My_packets package. this modules can help you to do some caculates.>>> Caculate. ===> can use the corresponding function functions under the Caculate module caculate.__class__ ( caculate.__reduce__ (caculate.__delattr__ ( caculate.__reduce_ex__ (caculate.__dict__  CACULATE.__REPR__ (caculate.__doc__  CACULATE.__SETATTR__ (caculate.__file__ Caculate.__sizeof__ (caculate.__format__ ( caculate.__str__ ( CACULATE.__GETATTRIBUTE__ ( caculate.__subclasshooK__ (caculate.__hash__ ( caculate.add (caculate.__init __ ( caculate.cut (caculate.__name__ caculate.div (caculate.__new__ ( caculate.multiply (caculate.__package__ >>> my_module1. ===> can use the corresponding functions under the My_module1 module My_ Module1. Salary ( my_module1.__name__my_module1. Work ( my_module1.__new__ (my _module1.__class__ ( my_module1.__package__my_module1.__ Delattr__ ( my_module1.__reduce__ (my_module1.__dict__ my_module1.__reduce_ex__ (my_module1.__doc__       MY_MODULE1.__REPR__ (my_module1.__file__ my_module1.__setattr__ (my_module1.__format__ ( my_module1.__sizeof__ (my_module1.__getattribute__ ( my_module1.__str__ (my_ Module1.__hash__ ( my_module1.__subclasshook__ (my_ Module1.__init__ ( my_module1.sayhi (
3. How to use the common module
• You can refer to your personal documents, which are not mentioned here first.
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1697787
"Python Tour" chapter III (IV): PYTHOH module