The Python language has very powerful functions. In addition to classes, there are also the concepts of modules and packages. This is a bit like perl. This article mainly introduces packages and modules, for more information, see the Python language. In addition to classes, there are also modules and packages. This is a bit like perl. Here we will briefly talk about packages and modules.
I. Python modules
The module is actually the concept of library (lib), but it not only contains a series of functions, but also can contain classes. python does not include C languages, include a file directly. The package is similar.
Python introduces two modules:
1,Import Module name (actually the file name is. py)
2,Module name = _ import _ ("module File Name (without extension )")
You can also use "import module name as alias"
For example:
The Code is as follows:
Test. py
#-*-Coding: gb18030 -*-
# Introduction module
Import test_mod
# Call functions in the module
Test_mod.my_func ()
# Classes in the call module
Tc = test_mod.test_cls ()
Tc. test_func ()
The source code of test_mod.py is as follows:
#-*-Coding: gb18030 -*-
Def my_func ():
Print 'I am a function in the module! '
Class test_cls:
Def test_func (self ):
Print 'I am a mothod in the class! '
To call a function or class in a module, you must use "module name. Class Name | function name.
It is precisely because of this feature that many operating systems exist in python. xx, sys. xx and other syntaxes, but it is not necessarily an object (the object and non-object appear very messy, or you can think that the module is also an object, but it is quite special ), this is a serious drawback of the python language, but when you get used to it, it is easier to understand python scripts.
Sys and OS are the most common modules in python.You need to know about them.
Ii. packages in Python
The package is actually an encapsulation of a series of modules to prevent conflicts between module names. For a standard python program, the general structure is:
The Code is as follows:
App. py
Appname
_ Init _. py
Son_pack1
_ Init _. py
Son_mod_1_1.py
Son_mod_1_2.py
Son_pack2
_ Init _. py
Son_mod_2_1.py
Son_mod_2_2.py
Son_mod_1.py
.......
If you have learned Java, it will be obvious that this is a method to find files by directory, but the difference is that each directory must contain _ init __. py otherwise, it will not be recognized as a sub-directory of the package.
You can specify or leave the file empty in three special variables: _ version _, _ all _, and _ path.
The methods for calling classes or functions in the package are:
Package name. Sub-package name. Class Name | function name
This is actually equivalent to the namespace in C ++ or C.
When calling a package, you must register the Directory and call methods of specific modules.
For example, to call son_mod_1_1.py
That is:
The Code is as follows:
Appname. son_pack1.son_mod_1_1
Another method is to use the form keyword. The method is:
The Code is as follows:
From appname. son_pack1 import son_mod_1_1
If you want to introduce all modules in appname. son_pack1:
The Code is as follows:
From appname. son_pack1 import *
In case of using import *, _ all _ must be specified in _ init _. py, for example:
The Code is as follows:
_ All _ = ["son_mod_1_1", "son_mod_1_2"]