Concise python Tutorial--c++ Programmer's Perspective (iii): module

Source: Internet
Author: User
Tags first string list of attributes

Modules and Packages

1 Python programs consist of packages, modules, and functions. A package is a collection of modules that are made up of a series. A module is a collection of functions and classes that handle a class of problems. A function is a piece of code that can be repeated multiple calls.

The 2 Python program is made up of modules. A module organizes a set of related functions or code into a file, and a file is a module. Modules consist of code, functions, and classes. Each module file is a fully-fledged namespace, and a module file cannot see the variable names defined by other files unless it explicitly imports that file, and the module file plays a role in minimizing the naming conflict.

3 The Import module uses the import and from statements (both implicit assignment statements) and the Reload function. Traps: Use the From import variable, and those variables happen to have the same name as the existing variables in the scope, and the local variables are silently overwritten; Using import does not matter.

The 4 package is a toolkit to accomplish a specific task, and the package is to implement the reuse of the program. The package must contain a __init__.py file that identifies the current folder as a package. By organizing the files into subdirectories according to the function, the package import will make the module play a more obvious role and make the code more readable.

5 You can import the module name, you can also specify the directory path (the directory of Python code is called the package), the package import is to turn the directory on the computer into another Python namespace, the package's properties are subdirectories and module files contained in the directory.

6 Package Import can occasionally be used to resolve import uncertainties when multiple program files with the same name are installed on a machine. The import package also uses the import and from statements.

Using the SYS module

If you want to reuse many functions in other programs, you need to use modules. A module is basically a file that contains all of the functions and variables you define. In order to reuse modules in other programs, the file name of the module must be the .py extension.

Modules can be entered from other programs to take advantage of its functionality. The purpose of a module is that it can reuse the services and functions it provides in other programs. The standard libraries included with Python are examples of such a set of modules. First, we'll learn how to use the standard library module.

    • First, we use the import statement input sys module. SYS is an abbreviated module of "system" that sys contains functions related to the Python interpreter and its environment. When Python executes import sys the statement, it sys.path looks for the module in the directory listed in the variable sys.py . If this file is found, the statement in the main block of the module will be run and the module will be able to be used by you. Note that the initialization process only takes place the first time we enter the module.
    • sysThe variables in the module are argv indicated by the use of dots-- sys.argv One advantage of this approach is that the name does not conflict with any variables that are used in your program argv . In addition, it clearly shows that this name is sys part of the module. A sys.argv变量是一个 list of strings that contain command-line arguments , which are parameters that are passed to your program using the command line. If you use the IDE to write these programs, look for a way to specify the command-line arguments of the program in the menu. (Press F6 under the Spyder)

    • Here, when we execute python using_sys.py we are arguments , we use the Python command to run the using_sys.py module, and the following content is passed as a parameter to the program. Python stores it in a variable for us sys.argv . Remember, the name of the script is always sys.argv the first parameter of the list. So, here, ‘using_sys.py‘ Yes, sys.argv[0] Yes ‘we‘ sys.argv[1] , ‘are‘ Yes sys.argv[2] and ‘arguments‘ Yes sys.argv[3] . Note that Python starts counting from 0, not starting with 1.
    • sys.pathList of directory names that contain input modules. The first string we can observe sys.path is empty-the empty string represents a part of the current directory sys.path , which is the same as PYTHONPATH the environment variable. This means that you can enter the module directly in the current directory. Otherwise, you have to put your module in one of the sys.path listed directories.



Byte-compiled. pyc files

It is relatively time-consuming to enter a module, so Python does some tricks to make the input module faster.

One method is to create a byte-compiled file that .pyc acts as an extension. Byte-compiled files are related to the middle state of the Python Transformation program (Do you remember how Python works?). )。 When you enter this module from another program the next time, the .pyc file is very useful-it will be much faster because the processing required for some of the input modules has been completed. In addition, the files compiled by these bytes are platform-independent.

From.. Import statement

If you want to enter the argv variable directly into your program (avoid hitting it every time you use it sys. ), you can use the from sys import argv statement. If you want to enter sys the names used by all the modules, then you can use the from sys import * statements. This is true for all modules.

In general, you should avoid using from..import import statements because this will make your program easier to read, and you can avoid conflicting names.

__name__ of the module

When a module is imported for the first time, the main block of the module will be run. If we only want to run the main block when the program itself is being used, and not run the main block when it is entered by another module, this can be done through the __name__ property of the module.

Each Python module has it, and __name__ if it is ‘__main__‘ , this means that the module is run by the user individually and we can do the appropriate work accordingly.

Make your own module

Each Python program is also a module. You have made sure that it has an .py extension. Remember that this module should be placed in the same directory as the program we entered it, or in sys.path one of the listed directories.

File dir/untitled0.py:

File dir/untitled1.py

The following is a from..import version that uses syntax.

The result is the same:

Dir () function

When you dir() provide a module name , it returns a list of the name of the module definition (functions, classes, and variables). If no argument is supplied, it returns a list of names defined in the current module .

First, let's take a look at the input sys module for use dir . We see that it contains a large list of attributes.

Next, we dir use it without passing arguments to the function--by default, it returns the list of properties for the current module. Note that the input module is also part of the list.

For dir The purpose of observation, we define a new variable a and assign it a value, and then examine it dir , and we observe that the same value is added to the list. We use the del statement to delete the variables/attributes in the current module, and this change is reflected in dir the output again.


dela note about--this statement is used to delete a variable/name after it is run. In this case, del a you will no longer be able to use the variable a -it is as if it had never existed. Results:

From:http://www.cnblogs.com/wei-li/archive/2012/03/25/2416291.html

Concise python Tutorial--c++ Programmer's Perspective (iii): module

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.