Python getting started --- three methods of module import and Chinese comments
Python has three modules to import functions. 1. Using the import module import modname: a module is a code segment that can be used interactively or accessed from another Python program. Once a module is imported, any common functions, classes, or attributes of the module can be referenced. You can use the functions of other modules in this way.
Import the module using the import statement. A reference to this module is created in the current namespace. this type of reference must use the full name. That is to say, when using a function defined in the imported module, it must contain the module name. Therefore, instead of using only funcname, you should use modname. funcname. Note: You can add the following to make Python support Chinese annotations.
# Coding: gbk or # coding: UTF-8 or ###-*-coding: gbk -*-
System import module
For example, if I create a m. py file in the home directory, We have imported many system modules, such as time and string. This is similar to include in c ++.
#!/bin/env pythonimport timeimport stringimport reimport socketimport threadingtime.sleep(10)print java
Import the private module m. py file
#! /Bin/env python # coding: gbk # coding: UTF-8 # The above loading supports the Chinese annotation def plus (a, B ): # XXX is a function description or use this is a test + a = a + B return a + B
Use import to load the m. py Module
#! /Bin/env python # coding: gbk # coding: UTF-8 use the sys module to import the specified Python file path import syssys. path. append (. /) import the Python File import mprint m. plus (12,3) print help (m. plus)
2. from modname import * mode loading Module
From modname import funcname
From modname import fa, fb, fc
Or from modname import *
Difference from the 1st method: funcname is directly imported to the local namespace, so it can be used directly without the limitation of the module name.
* Indicates that all the public objects (public objects) of the module are imported to the current namespace, that is, anything that does not start with "_" will be imported.
Modname is not defined, so modname. funcname does not work. In addition, if funcname has been defined, it will be replaced by the new version (the version in this import module. If the funcname is changed to point to another object, modname cannot be invisible.
Suggestion:
1) If you want to frequently access the attributes and methods of a module and do not want to enter the module name over and over again, use the from module import
2) If you want to selectively import some attributes and methods, instead of others, use the from module import
3) if the attributes and methods of a module have the same name as one of your modules, you must use the import module to avoid name conflicts.
4) Try to use less from module import *, because it is difficult to determine where a special function or attribute comes from, and it will make debugging and refactoring more difficult.
#! /Bin/env python # coding: gbk # coding: UTF-8 use the sys module to import the specified Python file path import syssys. path. append (. /) another way to import the Python file from m import plusprint plus () print help (plus)
3. Use the restrained function _ import _ in addition to the first two methods using the import keyword, we can also use the built-in function _ import _ () to import the module. The difference between the two is that the import must be followed by a type, and the _ import _ () parameter is a string, which may come from the configuration file, it may also be the result of an expression. For example
Mymodule = _ import _ ('module _ name ')
Note:
1) the content of the module is stored in a module File. For example, the content of mymodule should be stored in a mymodule. py directory under PYTHONPATH, unless implemented by C.
2) The package can organize several module namespaces. For example, A. B indicates A sub-module B in Package.
#! /Bin/env python # coding: gbk # coding: UTF-8 use the sys module to import the specified Python file path import syssys. path. append (. /) import the Python file my = _ import _ (m) print my. plus (12,3) print help (my. plus)