Python Learning (6) module and python Learning Module
Python module definition
As the program grows larger, several files need to be separated; functions, classes, and variables need to be reused in different files, rather than copying code. To meet these needs, Python provides a module.
Simply put, a module is a file that saves Python code. The module can define functions, classes, and variables. The module can also contain executable code.
A module is a file that includes Python definitions and declarations. The file name is the module name with the. py suffix. (Currently, the. py file saved by using Notepad ++ is a module)
The module name (as a string) can be obtained from the global variable _ name.
For example, you can create a file named fibo. py in the current directory with the following content:
1 # Fibonacci numbers module 2 3 def fib(n): # write Fibonacci series up to n 4 a = 0 5 b = 1 6 while b < n: 7 print(b, end=' ') 8 b = a + b 9 a = b - a10 print()
Note: Notepad ++ can be viewed in different views. Select to move to another view and view the following
Create 1. py file, such as module. py (. in the same directory as py), reference fibo import fibo, and execute the fib function under the fibo. fib (1000) (that is, the output of the fibonacci series in 1000), see below
Import Module
The module needs to be imported. Through the example above and the previous sections, we have come into contact with the import statements. Now we will introduce the import of the module in detail.
Import modulename1 [, moduleName2 [,... moduleNameN]You can use the import Statement to import one or more modules. The module import command is usually placed at the top of the script.
For more information, see the preceding example. The import fibo command does not directly import the function in the fibo file to the current instance. It only introduces the module name fibo. You can use the module name to access the function fibo. fib (1000)
You can define variables in the code to repeatedly use this function, for example:
Fib = maid # new function variable fib (20) fib (100)
From modname import name1 [, name2 [,... nameN]Import one or more specified parts from the module to the current namespace
Add a function in fibo. py. The Code is as follows:
1 def fib_l(n): # return Fibonacci series up to n2 result = []3 a = 04 b = 15 while b < n:6 result.append(b) 7 b = a + b 8 a = b - a9 return result
Import the specified part of the fiber _ L from the fiber o import, so that the module name will not be imported.
From modname import *All definitions in the import module (except those named after the underscore _ below) should not be used too much.
Positioning module
When you import a module, the Python parser searches for the module location in the following order:
1. Current Directory
2. If the directory is not in the current directory, search for each directory under the shell variable PYTHONPATH.
3. If none of them are found, Python will check the default path. In UNIX, the default path is/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.
Dir () function
The built-in function dir () is used to search for module definitions by module name and return a string storage list. If no parameter is called, the dir () function returns the name of the current definition.
This list lists all types of names: variables, modules, functions, etc. dir () does not list built-in functions and variable names, which are defined in _ builtin _.