Python modules, packages, class __python

Source: Internet
Author: User

Referencing in Python is a very simple thing, where you need to know three concepts to package, module, class. Class This is needless to say.

The module corresponds to a. py file, so module_name is the file that removes the file name from the. py file, which can directly define variables, functions, and classes.

So the package we can think of as a folder containing __init__.py and a series of. py files to distinguish between packages and ordinary strings.

Import Module_name

From package_name import module_name

From package_name Import *

So how do you import the Python interpreter to find the file location where the module is placed? The priority of Python looking for modules is as follows:

1. Current file directory

2. Environment variable Pythonpath

3, Sys.path (list type)

Sys.path is the list type, we can add the search path that the module imports by using the Insert (), append () method, such as:

Import Sys

Path = "..." # needs to be added

Sys.path.insert (0, path)

At the import module, the top level code for the module is executed once. If the module is import multiple times, such as import a, import B. Where the B module itself has import a, the top-level code is executed only the first time it is import.


A module can contain executable statements as well as a function definition. These executable statements are typically used to initialize the module. These statements are executed only when the module is imported for the first time. This is very important, and some people think that these statements will be imported multiple times, but they are not.

When the module is being imported, the Python interpreter generates the. pyc file in the same directory as the module file to speed up the program's startup speed. We know that Python is an explanatory scripting language, and. PYc is a compiled bytecode, which is done automatically without the programmer having to do it manually.


When you should use the From module import. If you want to frequently access the properties and methods of the module, and do not want to typing the module name again and again, use the From module import. If you want to selectively import certain properties and methods, and do not want other, use the From module import. If the module contains properties and methods that have the same name as one of your modules, you must use import module to avoid name collisions.

In addition to these situations, the rest is just a matter of style, and you'll see Python code written in two different ways.

Use the From module import * as little as possible, because it is difficult to determine where a particular function or property comes from, and it can make debugging and refactoring all the more difficult.
Other points:


Package

After creating dozens of modules, we may want to organize some similar files in the same folder, where we need to apply the concept of packages. packages correspond to folders, and packages are used in the same way as modules, and the only thing to note is that when folders are used as packages, folders need to contain __init__.py files, primarily to avoid using the folder name as a normal string. The contents of the __init__.py can be empty, generally used for some initialization of the package or set the __all__ value, __all__ is used in the From Package-name import * This statement, all the defined modules are exported.



We usually create a. py file (similar to Java, but Python does not need to build classes, is a modular carrier) as to how to organize a package, it needs to be functional.

In the C/c++/java, main is the starting point for program execution, and there are similar mechanisms in Python, but in a very different way: Python uses indentation to align the execution of your organization's code, and all code that is not indented (not function definitions and class definitions) is automatically executed at load, and the code, Can be considered Python's main function.

Each file (module) can write any code that is not indented. and automatically executes at load time, in order to distinguish between the main execution file and the file being invoked, Python introduces a variable __name__, when the file is called, the __name__ value is the module name, and when the file is executed, __ name__ as ' __main__ '. This feature, which provides excellent support for test-driven development, allows us to write test code in each module that runs only when the module is executed directly by Python, and the code and test are perfectly combined.

A typical Python file structure:


Python Import Module method

pythonThe module method contained in subdirectories is simpler, and the key is to find the path to the module file in the Sys.path.
Several common scenarios are described below:
(1) The main program and the module program in the same directory:
such as the following program structure:
'--src
|--mod1.py
'--test1.py
If you import module MOD1 in the program test1.py, you can use theImportMod1 or from mod1 import *;

(2) The directory where the main program resides is the parent (or grandparent) directory of the module's directory
such as the following program structure:
'--src
|--mod1.py
|--MOD2
| '--mod2.py
'--test1.py
If you import module MOD2 in the program test1.py, you need to establish an empty file __init__.py file in the Mod2 folder (you can also customize the Output module interface in the file). Then use the From MOD2.MOD2 Import * or import mod2.mod2.

(3) The main program to import modules or other directories in the upper directory (peer) module
such as the following program structure:
'--src
|--mod1.py
|--MOD2
| '--mod2.py
|-| sub
| '--test2.py
'--test1.py
If you import modules Mod1 and MOD2 in the program test2.py. First you need to create a __init__.py file (Same (2) under MOD2), SRC without having to create the file. The method is then invoked as follows:
The following programs execute in the same directory as the program files, such as test2.py on CD sub; then execute Python test2.py
While test1.py is in CD src; then execute Python test1.py; It is not guaranteed to execute Python sub/test2.py successfully under the SRC directory.
Import Sys
Sys.path.append ("..")
Import Mod1
Import MOD2.MOD2

(4) from (3) it can be seen that the import module is the key to the SYS.PATH environment variables according to the value of the specific module to find the path. Here are just three simple things to do.


Transmission door: http://blog.chinaunix.net/uid-26602509-id-3499026.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.