Import of functions in the module or module in Python:
1. Import: Module name 2. Import: Module name as new name 3. From Module name import function name
Note: 1. Import the entire module. When using functions in the module, you must add "module name" before function description. "2. From is to import the functions in the module. when the drug is imported into multiple functions, use", "to separate each function. Use '*' instead of all functions in the import module. You can directly use the function without adding "module name." 3. You can use the built-in function reload to reload the module in Python. Reload can re-load the module without shutting down Python when the module is modified. When using it to re-load the module, the module must have been imported in advance.
Write a module:You can define the variable: Name file name: test. PY content: Def: Show (): defines a function print "this is a test" name = "name test" to define a variable using the module File Name: Use. PY content: Import test from test import show test. show () show ()
Module search path:When importing a module, the python interpreter first searches for the imported module in the current working directory. If it is not found, it is in the directory specified by the PATH variable in the SYS module (import sys. path). If the imported module is not found, an error occurs. in the script, you can. add the module search path to the PATH variable. Add the modules directory under the current directory to SYS. in the PATH variable, for example: Import OS, sys modulepath = OS. SYS. path. append (modulepath)
Module runs independently: __name _ attributeEach Python script has an attribute of _ name _ during running. In the script, you can determine the attribute value of _ name _.
Import module or run it independentlyCan run correctly. 1. If the Python script is used as the import module, the _ name _ attribute is set as the module name. 2. If the Python script is run independently, the _ name _ attribute is set to _ main _. Therefore, you can use the _ name _ attribute to determine the running status of the Python script.
Dir () function: If you need to obtain the names of all variables and functions defined in the imported module, you can use the Dir () function's prototype: dir ([object]) Where object is an optional parameter. If the parameter is null, the Dir () function returns a list Of all names of the current script. Eg: Import sys Dir (sys)
Variable length parameter:You can use a variable-length function to save all its parameters in a single tuple. You can use a for loop to process the parameters in the function. To declare a variable length parameter, you only need to define a parameter starting with "*" to define def listappend (* list): Definition of Variable Length Parameter
L = []
For I in list:
L. Extend (I)
Return L
A = [1, 2, 3]
B = [4, 5, 6]
C = [7, 8, 9, 0]
"Two Parameters"
Print listappend (A, B)
Print listappend (a, c)
Print listappend (B, c)
"Three Parameters"
Transfer of function parameters in print listappend (a, B, c) Python: 1. PASS Parameters in the order of declared functions. 2. PASS Parameters by name. It is convenient to use the default parameter values. Def fun (x, y, z ):
Return X + Y-z
The order of fun (, 3) x = 1, y = 2, Z = 3 parameters is passed.
Fun (Z = 1, x = 2, y = 3) parameter name transfer fun (1, Z = 3, y = 2) Legal fun (Z = 3, y = 2) invalid. The order is invalid for fun (5, Z = 6, x = 7 ).
Note: parameters passed in sequence must be placed before parameters passed by parameter name, and there must be no duplicates.