Modules allow you to logically organize your Python code snippets.
Assigning the relevant code to a module will make your code better and easier to understand.
The module is also a Python object, with a random name attribute used to bind or reference.
Simply put, the module is a file that holds the Python code. Modules can define functions, classes, and variables. The module can also contain executable code.
Import
To use a Python source file, simply execute the import statement in another source file, and when the interpreter encounters an import statement, the module will be imported if it is in the current search path.
1 # Coding=utf-8 2 # !/usr/bin/python 3 4 # Import Module 5 Import Support 6 7 # you can now invoke the functions contained in the module. 8 Support.print_func ("Zara")
A module will only be imported once, no matter how many times you execute the import. This prevents the import module from being executed over and over again.
From...import
Imports a specified section from the module into the current namespace
To import the Fibonacci function of the module fib ,the from fib import Fibonacci, this declaration does not import the entire FIB module into the current namespace. It only introduces the Fibonacci individual in the FIB to the global symbol table of the module that executes the declaration.
from...import*
It is also possible to import all the contents of a module into the current namespace, form ModName import*, which provides an easy way to import all the items in a module. However, such statements should not be used too much.
When importing a module, the Python parser's search order for the module location is:
- Current directory
- If it is not in the current directory, Python searches for each directory under the shell variable Pythonpath
。
- If none are found, Python looks at the default path. Under UNIX, the default path is typically/usr/local/lib/python/
The module search path is stored in the Sys.path variable of the system module. The variable contains the current directory, Pythonpath, and the default directory determined by the installation process.
--------------------------------------------------------------------------------------------------------------- ---------------------------------
Global variables Globals varname
Dir () function
The returned list contains all the modules, variables and functions defined in a module.
1 # Coding=utf-8 2 # !/usr/bin/python 3 4 # Import the built-in math module 5 Import Math 6 7 content = dir (math)8 9print content;
['__doc__','__file__','__name__','ACOs','ASIN','Atan', 'atan2','Ceil','Cos','cosh','degrees','e','Exp', 'fabs',' Floor','Fmod','Frexp','Hypot','Ldexp','Log','log10','MODF','Pi','POW','radians','Sin','Sinh', 'sqrt','Tan','Tanh']
The special string variable __name__ points to the name of the module, and __file__ points to the import file name of the module.
Globals () and locals () functions
Depending on where they are called, the Globals () and locals () functions can be used to return names in the global and local namespaces.
If locals () is called inside the function, all the names that can be accessed in the function are returned.
If Globals () is called inside the function, all the global names that can be accessed in the function are returned.
The return type of two functions is a dictionary. So names can be extracted with the keys () function.
Reload () function
When a module is imported into a script, the code at the top-level part of the module is executed only once.
Therefore, if you want to re-execute the code in the top-level part of the module, you can use the reload () function. The function will re-import the previously imported modules
Reload (Hello) module name
Python Package
A package is a hierarchical file directory structure that defines a Python application environment consisting of a module and a sub-package, and a sub-package under a sub-package.
Understand:
A pots.py file in the phone directory, as well as two other files that hold different functions
- phone/pots.py contains the function pots ()
- phone/isdn.py contains function Isdn ()
- phone/g3.py contains function G3 ()
When you import the phone, in order to be able to use all functions, you need to use explicit import statements in __init__.py
1 from Import Pots 2 from Import Isdn 3 from Import G3
When you add the code to __init__.py, the classes are all available when you import the phone package.
1 # Coding=utf-8 2 # !/usr/bin/python 3 4 # Now import your Phone package. 5 Import Phone 6 7 phone.pots () 8 phone.isdn () 9 phone.g3 ()
You can place many functions, or you can define Python classes in these files, and then build a package for those classes.
Python Learning note eight: Modules