Introduction to Python Programming (4) modules and packages

Source: Internet
Author: User
The Python language feature is very powerful, in addition to the class, there is the concept of modules and packages, which is a bit like Perl, here is a brief talk about packages and modules.

First, the module in Python

Module-is actually what we call the concept of Library (Lib), but it can not only contain a series of functions, can also contain classes, Python is not like the C language, directly include a file, the package is the same thing.

There are two ways in which Python introduces modules:

1,Import module name (the actual corresponding is the file name. py)

2. Module name = __import__ ("Module file name (without extension)")

You can also use the Import module name as Alias.

For example:

The code is as follows:

test.py
#-*-coding:gb18030-*-
#引入模块
Import Test_mod

#调用模块里的函数
Test_mod.my_func ()

#调用模块里的类
TC = TEST_MOD.TEST_CLS ()
Tc.test_func ()

test_mod.py source code 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! '

The function or class in the calling module must use the "module name. Class name | function name" method.

Because of this feature, Python is a lot of os.xx, sys.xx and other grammars, but it is not necessarily the object (object and non-object is very confusing, or can think of the module is also an object, but rather special), which is a serious disadvantage of the Python language, But when you get used to this, it's easier to read the Python script.

sys and OS are the most commonly used modules in Python and need to know about them.

Ii. packages in Python

A package is actually a package of modules to prevent collisions between module names, and for a standard Python program, the usual 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 learn Java it will be obvious that this is a way to find files by directory, but the difference is that each directory must take __init__.py otherwise it will not be recognized as a subdirectory of the package.

In the __version__, __all__, __path__ Three special variables, you can specify, you can also let this file is empty.

The method for calling a class or function in a package is:

Package name. Name of the child package. Class Name | function name

This is actually equivalent to namespaces in C + + or C #.

When calling packages, you need to register directories and call methods for specific modules

For example, to invoke son_mod_1_1.py

So that is:

The code is as follows:

Appname.son_pack1.son_mod_1_1

Another way to do this is to use the form keyword, in the following way:

The code is as follows:

From Appname.son_pack1 import son_mod_1_1

If you want to introduce all the modules in the Appname.son_pack1, then:

The code is as follows:

From APPNAME.SON_PACK1 Import *


For cases where import * is used, it must be specified in __init__.py with __all__, such as:

The 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.