References to modules in Python

Source: Internet
Author: User

I. Definition of the module

definition

The Python module, which is a python file that ends with a. Py, contains Python object definitions and Python statements. Modules allow you to logically organize your Python code snippets and assign related code to a module to make your code better and easier to understand. Modules can define functions, classes and variables, and modules can contain executable code.

Purpose

Improved reusability

usage

1) Import Module name

Note: If there is a class in the module name, you must instantiate the class to invoke the method in the class: module name. Class name (). Method

2) from the module name import corresponding class or function (usually called the method inside the class)

two. Import the module

Recommended Python code style:

♦ Import packages and modules using import x

♦ Use from x import Y to import the module, where x is the package name and Y is the module name without the package name

♦ Using from x import y as Z, if two modules to be imported are called Y or y is too long

♦ Use from x.y import Z imports module Z to use the full path of the package (Y is the sub-package under X packet), do not nest, after import to ensure that only the module, without package name

Not recommended import method (full Import): From math import *

Packages and folders

For better organization of modules, use packages to manage

The package is a folder, but the __init__.py file must exist under the folder

In the simplest case, you only need an empty __init__.py file to

New Python Package

The __init__.py file is automatically generated under the package

three. Relative path import

Python3, import the other Python files under the same project, using relative paths (the root directory is the project directory)

Project when you create a project, for the root directory

module in the root directory: Import X

module in a folder under the root directory: from Y import X

Example 1

The module name is in the root directory of the project

# Introducing Modules Import Test # Introduction Class  from Import TTT

Example 2

module in a package in the root directory of the project

# Introducing Modules  from Import Test # Introduction Class  from Import TTT

References to modules in Python

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.