Python-model&package Module and package detailed

Source: Internet
Author: User

http://blog.csdn.net/pipisorry/article/details/43313197

Modules and Packages

1 Python programs consist of packages, modules, and functions. A package is a collection of modules that are made up of a series. A module is a collection of functions and classes that handle a class of problems. A function is a piece of code that can be repeated multiple calls.

The 2 Python program is made up of modules. A module organizes a set of related functions or code into a file, and a file is a module . Modules consist of code, functions, and classes. Each module file is a fully-fledged namespace, and a module file cannot see the variable names defined by other files unless it explicitly imports that file, and the module file plays a role in minimizing the naming conflict.

3 The Import module uses the import and from statements (both implicit assignment statements) and the Reload function.

The 4 package is a toolkit to accomplish a specific task, and the package is to implement the reuse of the program. The package must contain a __init__.py file that identifies the current folder as a package . By organizing the files into subdirectories according to the function, the package import will make the module play a more obvious role and make the code more readable.

5 You can import the module name, you can also specify the directory path (the directory of Python code is called the package), the package import is to turn the directory on the computer into another Python namespace, the package's properties are subdirectories and module files contained in the directory.

6 Package Import can occasionally be used to resolve import uncertainties when multiple program files with the same name are installed on a machine. The import package also uses the import and from statements.


Import package in Python

1 from import statement

If you want to enter the argv variable directly into your program (avoid hitting it every time you use it sys. ), you can use the from sys import argv statement. If you want to enter sys the names used by all the modules, then you can use the from sys import * statements. This is true for all modules.

2 two ways to import modules, import differs from From...import

Like import, the module being imported is still executed and executed only once.

The essence of the FROM * * Import (the difference between import and from import)

When you import a module in from import, Python creates a new named namespace in the current module's namespace.

That is, "fromT2 import var1" is equivalent to:

Import T2

var1= t2.var1

in this process, there is a implicitly assigned value the process

Because of the Python assignment operation characteristics (referencing the nature of Python variable names), changes to "var1" in the current code do not affect the value of "var1" in t2.py. At the same time, changes to var1 in t2.py are not reflected in the current code.

Traps: Use the From import variable, and those variables happen to have the same name as the existing variables in the scope, and the local variables are silently overwritten; Using import does not matter.

3 Import scipy Issues in the module note

"scipy-science python"


__name__ of the module

When a module is imported for the first time, the main block of the module will be run. If we only want to run the main block when the program itself is being used, and not run the main block when it is entered by another module, this can be done through the __name__ property of the module.

Each Python module has it, and __name__ if it is ‘__main__‘ , this means that the module is run by the user individually and we can do the appropriate work accordingly.



from:http://blog.csdn.net/pipisorry/article/details/43313197

Ref:http://www.cnblogs.com/wei-li/archive/2012/03/25/2416291.html


Python-model&package Module and package detailed

Related Article

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.