The Use of Python Function and Module
- 2021-12-12 04:47:05
- OfStack
1. The role of functions
Martin Fowler, a master programmer, once said: "Code has many bad tastes, and repetition is the worst one!".
Repeated functions can be encapsulated in a function module called "function" to implement single 1, or code snippets of associated functions.
2. Define the function
The rules for defining functions are as follows:
Function code block todef
Keyword beginning, followed by function name and parentheses ().
Function name naming rules and variable naming rules are 1
return
Keyword is used to exit the function and optionally return 1 expression to the caller.
The syntax is as follows:
def Function name (parameter list) :
Function body
return Statement
# Definition 1 Functions Put two parameters inside
def sum(arg1, arg2):
# Return 2 The sum of three parameters ."
total = arg1 + arg2
return total
# Call sum Function
sumNum = sum(10, 20)
print(sumNum) // 30
3. Parameters of the function
In addition to basic operations such as defining and calling functions, parameters can also be used in functions. The parameters of a function are simply variables that can be used in the function body of a function. Unlike variables, such variables are passed in before the function body.
3.1 Default Values for Parameters
When a function is called, if no parameters are passed, the default parameters are used.
def printInfo(name, age=12):
" Print any incoming strings "
print(" Name : ", name)
print(" Age : ", age)
return
# Call a Function
printInfo(age=10, name=" Xiao Hong ")
print("------------------------")
printInfo(name=" Xiao Ming ")
3.2 Variable parameters
Variable parameters mean that when a function is called, 0 or any number of parameters can be passed into the function.
# Represented by an asterisk expression stamp Can be received 0 One or any number of parameters
def printInfo(*stamp):
# Variable parameters can be placed in for The value of each parameter is taken out of the loop
for i in stamp:
print(i)
return
printInfo("1 Bowl week ")
printInfo(1, 2, 3, 4, 5, 6, 7)
printInfo("hello", "hi")
4. Manage functions with modules
In the same 1
.py
If there are two function names in the file that have the same name, because
Python
Without the concept of function overloading, the following definition overrides the previous definition, which means that only one of the two functions with the same name actually exists. At this time, the function of the module is reflected.
Python
Each file in represents one module (
module
), you can have functions with the same name in different modules, and you can use a function through
import
Keyword is introduced into the specified module to distinguish each function.
Example: Define two modules, which are
module1
And
module2
And then test the incoming process
module1
def printinfo():
print(" This is the first 1 Modular printinfo Function ")
module2
def printinfo():
print(" This is the first 2 Modular printinfo Function ")
Use the import keyword to directly introduce:
import module1
import module2
# Add by module name . The way to introduce function names
module1.printinfo()
module2.printinfo()
Rename the introduction using the import... as keyword:
import module1 as m1
import module2 as m2
# You can add the renamed name . The way to introduce function names
m1.printinfo()
m2.printinfo()
Introduces a function using the from... import... as keyword rename:
from module1 import printinfo as p1
from module2 import printinfo as p2
# You can add the renamed name . The way to introduce function names
p1()
p2()
It is worth noting that if as is not used for renaming here, there are two
printinfo
Will result in an error, so you need to rename it
return
0
Usage of
__name__
Yes
Python
1 implied variable in which it represents the name of the module. If you print it directly,
__name__,
Will appear
__main__。
4.1 Sample code module. py
def printinfo():
print(" This is the content of the function ")
print(__name__)
print(" This is outside the module function ")
Output:
# Definition 1 Functions Put two parameters inside
def sum(arg1, arg2):
# Return 2 The sum of three parameters ."
total = arg1 + arg2
return total
# Call sum Function
sumNum = sum(10, 20)
print(sumNum) // 30
0
If the
module.py
When introduced as a module, the printed result is the name of the module (without path and extension)
Sample code
import module
Output:
module
This is outside the module function
At this time, the output result is not 1. Simply put, in
module.py
In one's own eyes
name
Is
main
However, in the eyes of other documents,
name
Is
module
.
Put
module.py
Transformation 1
# Definition 1 Functions Put two parameters inside
def sum(arg1, arg2):
# Return 2 The sum of three parameters ."
total = arg1 + arg2
return total
# Call sum Function
sumNum = sum(10, 20)
print(sumNum) // 30
1
Output:
This is the content of the function
__main__
This is outside the module function
When other files are introduced
import module
Output:
This is the content of the function
Because other files will not execute the code in the module when the if condition is true because the name of the module is module instead of __main__
return
0
It is very useful in the actual scene. 1. In the process of writing the module, you must test it. When you import it, you must not print out the test results. If you delete it, you have to write it again when you want to improve the module. At this time,
return
0
It came in handy
5. Scope of variables
Look at the following code:
# Definition 1 Functions Put two parameters inside
def sum(arg1, arg2):
# Return 2 The sum of three parameters ."
total = arg1 + arg2
return total
# Call sum Function
sumNum = sum(10, 20)
print(sumNum) // 30
2
According to this code, understand the scope of variables
At this time, in the function
test_nested
Calling a, b and c in vivo will not report any errors.
If the b variable is called outside the function, it will be reported
print(b)
NameError: name 'b' is not defined
Error of undefined variable b, because b is a local variable, belongs to local scope, and cannot be accessed outside the function
If in
test
Calling c in the function body will report
# Definition 1 Functions Put two parameters inside
def sum(arg1, arg2):
# Return 2 The sum of three parameters ."
total = arg1 + arg2
return total
# Call sum Function
sumNum = sum(10, 20)
print(sumNum) // 30
4
Undefined error of variable c, because c is in the
test_nested
Function body, for
test
Function, but for the
test
Inside the function
test_nested
Function, the variable b belongs to nested scope, and in the
test_nested
We can access it in the function.
If passed
Python
2
To promote b to a global variable, the b variable called outside the function will print normally
nonlocal
Keyword can only work on local variables, and always find the variable in the nearest upper local scope
# Definition 1 Functions Put two parameters inside
def sum(arg1, arg2):
# Return 2 The sum of three parameters ."
total = arg1 + arg2
return total
# Call sum Function
sumNum = sum(10, 20)
print(sumNum) // 30
5
Without this
nonlocal
Keyword then function body
inner
Print
222
, function body outer printing
111
After that, two 222 are printed directly
Python
When finding a variable, it will follow the "local scope"-
>
"Nested Scope"--
>
"Global Scope"--
>
"Built-in scopes," which are the identifiers built into Python, previously used
input
,
print
,
int
All belong to built-in scope.