Python module and Package entry method

Source: Internet
Author: User

Tag: is the name class written to test default Col div color

First, the module import

What is a module? In fact, the module is a py file.

There are several ways to import modules, different import methods, the use of functions in the module are not the same.

#1. Import Module Name:the calling method of the inside function, the module name. function name ()ImportTimetime.sleep (1)#2. Name of import function from module namefunction Call Method: function name () fromTimeImportSleepsleep (1)#3. From Module name import *function Call Method: function name () fromTimeImport*Sleep (1)#4. Import Module name as Aliasfunction Call Method: Alias. Function name ()ImportTime as Tt.sleep (1) 

Next, let's introduce the __all__ variable. It is used as follows: adding __all__ to the module file is actually a list of the names of each function. When this module is called with the From module name import *, what functions are imported into __all__, and if __all__ is not defined, all functions are imported by default.

This is the test module

def PrintA ():     Print ('AAAA') def printb ():     Print ('BBBB') __all__=['printA']

Here is the call to test

 from Import *PrintA ()    #AAAA# if called, PRINTB () error

What is a package?

The so-called package is actually a few py files, and contains a __init__ file.

Here's how to import packages:

# 1. From Package name  import  module name Call method: module name. function name ()#2, Import package name. module Name  Calling method: Package name. module name. function name ()#3, from  package name  import   Call method: module name. function name () Note: Use this if the __init __ File writes the __all__ variable, holds the module name to import, does not write the default nothing to import #4, the From Package name. module Name Import * Call Method: function name ()

Python module and Package entry method

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.