Introduction to Python programming (4) modules and packages

Source: Internet
Author: User

The Python language is very powerful. In addition to classes, there are also the concepts of 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:Copy codeThe 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:

Copy codeThe 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:Copy codeThe Code is as follows: appname. son_pack1.son_mod_1_1

Another method is to use the form keyword. The method is:

Copy codeThe Code is as follows: from appname. son_pack1 import son_mod_1_1

If you want to introduce all modules in appname. son_pack1:

Copy codeThe Code is as follows: from appname. son_pack1 import *


In case of using import *, _ all _ must be specified in _ init _. py, for example:

Copy codeThe Code is as follows: __all _ = ["son_mod_1_1", "son_mod_1_2"]

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.