Small ape circle python learning - using modules What are the benefits?

What is a module?
During development of a computer program, with the program code you write more and more, in a file, the code will become longer and more difficult to maintain.

In order to write code maintainable, we put a lot of function groups, which are placed in a separate file, so each file contains the code is relatively small, many programming languages ​​have adopted this way of organizing code. In Python, a file can be called a .py module (Module).

What are the benefits using a module?

The biggest benefit is greatly improved maintainability of the code. Secondly, you do not have to write code from scratch. When a write module is completed, it can be referenced elsewhere. When we write programs, often reference other modules, including a built-in Python modules and modules from third parties.

Also avoid using the module function names and variable name conflicts. Each module has a separate namespaces, so functions and variables of the same name can exist in different modules respectively, so we own in the preparation module, regardless of the name conflict with other modules

Classification module
module is divided into three types:

Built-in standard modules (also known as the standard library) execution help ( 'modules') to see a list of all module comes with python

3rd party open source module, the module name by pip install networking installation

Custom Modules

Import & module calls
import module_a # Import
from Import Module xx
from module.xx.xx after xx as rename # Import to import heavy command
from module.xx.xx import * # import all methods in a module is not recommended
module_a.xxx # call
Note: Once the module is called, which is equivalent to execute another py file code in
a custom module
the most simple, create a .py file, you can call module, you can import in another program

Module search path

Found himself writing module can only be imported at the current path, the directory for a re-import your own modules on an error saying can not find it, which is why?

This is related to the import module search path

SYS Import
Print (sys.path)
output (note the different possible output of the computer is not the same)

[ '', '/Library/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/ Library / Frameworks / Python.framework / the Versions / 3.6 / lib / python3.6 / lib-dynload ',
' /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']
you import a module when, Python interpreter will follow the order of the list above to go down in order to match the module name each directory you want to import, just to match the module name in a directory, import immediately, no longer continue in the future to find.

Note that the first element of the list is empty, that means the current directory, so your own custom module is a priority in the current directory will be imported.
If you want to create our own modules can be called at any place, it would have to make sure you find the module file, at least in the list of modules path.

We generally write their own modules on with a "site-packages" directory words, we downloaded from the Internet to install a variety of third-party modules are generally placed in this directory.

Exercise:
to write a module, and make sure your python code to import this module at any one place.

Guess you like

Origin blog.csdn.net/weixin_44867000/article/details/91411226