The role of the python in __init__.py, module and package

Import behavior control package:

  • 1, the current file is a declaration of the packet can be imported;
  • 2, if the current packet a plurality of files .py__ all__ = [ '模块名'],也就是form XXX import YYY

 

 

module和package

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 .pyfile is called a module (Module1) .

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. 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. But also pay attention, try not to conflict with the built-in function name.

If the module is written in the name of different people how to do the same? To avoid module name collisions, Python and the introduction of the method according to the organization directory module, called the package (Package Penalty for) .

Guess you like

Origin www.cnblogs.com/yanhuaqiang/p/11539711.html