The concept of Python modules and packages

Source: Internet
Author: User
Tags assert shuffle

Module:
With the development of the program, more and more functions, in order to facilitate management, we usually divide the program into a file, so that the structure of the program is clearer and easier to manage. At this point, we can not only use these files as scripts to execute, but also as a module to import into other modules, to achieve the function of reuse
One import:
1. Execute the corresponding file
2. Introducing variable names
To prevent multiple re-imports, the Python optimization means:
After the first import, the module name is loaded into memory, and the subsequent import is simply adding references instead of resetting the module

two the import module has a separate name and does not conflict with the global variables of the current function

three How to alias the module: Import time as Tim Print (Tim.time) from time import time as Hello

Four can import multiple modules in one row import sys,os,re from spam import Read1,read2,money

Five form ... import vs. import
From ... import ... The disadvantage of using more convenient is that it is easy to conflict with the name in the current execution file


six sys.path Initialize temporary modification function from the following location (permanent modification modified with system environment variable)
1. Directory where the files are executed
2. PYTHONPATH (contains a list of directory names, as with the shell variable path syntax)
3. Rely on the default setting when installing
4.BASEDIR use __file__ file to find the current file directory via Os.path.abspath (__file__)
Locate the absolute path directory of the file on the ancestor of Os.path.dirname (Os.path.dirname (__file__))

Seven It should be emphasized that our custom module name does not duplicate the module name of the Python standard library

Eight
1. Module names are case-sensitive, foo.py and foo.py represent two modules
2. You can use the-O or-oo to convert Python commands to reduce the size of the compilation module
-O conversion will help you remove the Assert statement
-oo conversion will help you get rid of ASSERT statements and __doc__ document strings
Because some programs may rely on ASSERT statements or document strings, you should confirm that you need to
Use these options in case of a
3. Reading instructions from the. pyc file at a speed is no faster than reading instructions from a. py file, and the. pyc file is faster when the module is loaded.

4. Only use the import statement to automatically compile files into. pyc files, specifying run scripts on the command line or standard input does not generate such files, so we can use the Compieall module to create. pyc files for all modules in a directory


Package
Used to organize the module function clearly to store separately

Random Module
Random.random () 0-1 floating-point number
Random.randint (1,3) take 1-3 between the plastic [1,3]
Random.randrange (1,3) [1,3)
Random.choice (Iterative object, 2) in which a random value of the second parameter is set to select several parameters
L = [1,2,3,4,5,6]
Random.shuffle (L)
Shuffle in order of disruption

The concept of Python modules and packages

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.