Basic python Functions
Significance of Using Functions
First of all, we must realize that functions are very important in python usage. For some code that is frequently repeated when we write code, we often use functions to solve such problems.
This is because functions have great advantages in solving code reuse, and the operations of these defined functions when they are faced with code modification also reduce the workload of a large part, and the program is quite readable, so there are so many benefits for functions. Let's use the functions.
How to Use Functions
Before using a function, you must define it before calling it.
Def func (arg1, arg2,): 'description' # function body return
Define functions
(1) Use the def keyword in Python to define the function. You do not need to specify the type of the returned value.
(2) The args parameter can be zero, one, or more. You do not need to specify the parameter type for the function parameter, because the variables in Python are weak, python automatically maintains its type based on the value.
According to the number of args, functions can be divided into no-Argument functions, with parameters and empty functions.
(3) The return statement is optional and can appear anywhere in the function body. If there is no return statement, NONE is automatically returned. If there is a return statement, however, if no expression or value is followed by return, NONE is returned. If one value is returned, one value is returned. If multiple values are returned, one tuples are returned. Note: Unlike yield, when the return operation ends, the subsequent values are not returned.
Global variables and local variables are involved in the function definition stage.
X = 10000 # define global variable def foo (): x = 1 # local variable print (x) foo ()
A local variable establishes a link when calling a function and terminates the link when it ends.
Participate in real parameters
Parameters in brackets are used to define functions, while parameters in brackets are used to call functions. It can be understood that the variable value of the real parameter is assigned to the form parameter, which is similar to the value assignment of the variable. Of course, only the function with parameters can actually participate in the real parameter.
def foo(x): x.append(4) print(x)x = [1, 2, 3]foo(x)
Here, the real parameter uses a list and the original list l is changed in the function. The experiment environment here is relatively simple. If such a variable real parameter is used in actual production, is not supported, which often leads to some faults that are difficult to locate. Therefore, it is recommended that the real parameters be of an unchangeable data type.
Location parameters and default parameters
Based on location parameters:
def foo(x,y): res=x+y return resprint(foo(5,10))
You can also assign values by specifying the default parameters:
def foo(x,y): res=x+y return resprint(foo(x=5,y=10))
Or use both of them together:
def foo(x,y): res=x+y return resprint(foo(5,y=10))
The default parameter is the default value used when the parameter is defined.
def foo(x,y=10): res=x+y return resprint(foo(5))
Dynamic Parameters
"*" Before the parameter: In the function, the extra parameters are converted into a single tuple.
def func (*args): print(args)func(123,1,2,'a')
Here, args is a tuples containing all elements.
** Kwargs is in the form of keywords. Redundant keywords are passed to kwargs, and the result is in the dictionary form.
Def func (x, ** kwargs): print (x) print (kwargs) func (1, y = 2, a = 3, B = 4) output result: {'A': 3, 'B': 4, 'y': 2}Namespace and scope
When defining variable names and function names, there is actually an address space to store, so you can get it directly from the namespace when calling and using it.
Python has three namespaces: built-in namespace, global namespace, and local namespace.
The scope of the global namespace and the built-in namespace is called the global scope, and the scope of the local namespace is called the local scope.
When a variable is needed inside the function, the local area namespace inside the function is searched. If not, go back to the global namespace to find the variable, and then go to the built-in namespace to find the variable.
Function nesting and closure Functions
Function nesting means that a function calls other functions internally.
For example, a function is larger than multiple values:
def my_max(x,y): res=x if x>y else y return resdef my_max4(a,b,c,d): res1=my_max(a,b) res2=my_max(c,d) res3=my_max(res1,res2) return res3print(my_max4(3,8,12,6))
Here, the function is passed as a parameter, and the latter function calls the previous function to complete the requirement. This is the basic application of nested functions.
In Python, functions can be operated as objects, such as being assigned values and passed as parameters.
A closure function is also a nested function. It must first be an internally defined function that references external scopes rather than global scope names.
The basic form of closure functions:
x=1def f1(): def f2(): print(x) return f2f2=f1()f2()
Run f1 in parentheses and return an address value of f2. f2 is the closure function. Run f2 to print the value.
Code for crawling the python Official Website:
from urllib.request import urlopendef f1(url): def f2(): print(urlopen(url).read()) return f2python=f1('http://www.python.org') python()
There are many application scenarios for closure functions in python, including the decorator to be mentioned later and simple crawling to webpages.