tags: Study notes
2021/2/4
# Example:
def fun():
def fun1():
pass
return fun1
# Example: Extract List List1
list1 = [1,2,3,4,5,6,7,8,9,10]
def fun1(i):
if i % 2 == 0
return True
def fun(fun1,list1) :
list2 = []
for i in list1:
if fun1(i):
list2.append(i)
retrun list2
print(fun(list1))
FILTER () needs to pass two parameters and press the set rules to filter the required data
list1 = [1,2,3,4,5,6,7,8,9,10]
def fun1(i):
if i % 2 == 0
return True
print(list(filter(fun1,list1)))
Specially used as some simple functions
Syntax: Lambda parameter: expression
# Example 1
print((lambda a,b: a+b )(10,20))
# Example 2
r = lambda a,b: a+b
print(r(10 ,20))
# Example Extract List List1 even
list1 = [1,2,3,4,5,6,7,8,9,10]
r = lambda i: i % 2 ==0
print(list(filter(r,list1)))
Taking a function as a return value is also a high-order function. We also call it a closure.
Benefits of closed bags
• Create some variables that only the current function can be accessed through the closure
• You can hide some private data into the closure.
Conditions forming a closed bag
• Function nested
• Return the internal function as the return value
• Internal functions must be used to use variables of external functions
The first characteristic of the closure: variables are not destroyed
def func_out(num1):
def func_inner(num2): #
result = num1 + num2 #
print(result)
return func_inner # Return the internal function as the return value
f = func_out(1) # n1 1
f(2) #Function call, send Num2 one 2
operation result """3
f(3)
operation result """4
The second characteristic of the closure: variable cannot be changed
def func_out(num1):
def func_inner(num2): #
num1 = 10
result = num1 + num2 #
print(result)
print(num1) # This can be clearly seen in Num1 changes
func_inner(2)
print(num1)
return func_inner # Return the internal function as the return value
func_out(1) # Function object is FUNC_OUT function call is func_out ()
operation result """
1
12
1
Let the external variables can be modified in the closure
def func_out(num1):
def func_inner(num2): #
# , use the external variable Num1 here
nonlocal num1
# The original meaning of this is to modify the value of the external variable Num1, which is actually a re-assignment.
num1 = 10
result = num1 + num2 #
print(result)
print(num1)
func_inner(2)
print(num1)
return func_inner # Return the internal function as the return value
func_out(1) # Function object is FUNC_OUT function call is func_out ()
operation result """
1
12
10
We can complete the demand directly by modifying the code in the function, but there will be some problems
def add(a, b):
return a + b
def fun_out(fn, *args, **kwargs):
def fun_inner():
print('Function start execution')
r = fn(*args, **kwargs)
print(r)
print('Function execution is ")
return fun_inner
f = fun_out(add, 1, 2)
f()
operation result """
Function starts
3
The function is executed
, The function may be expanded without modification of the original function down through decorators
In development, we are to extend the functionality of a function by a decorator
#Energy decorator
def fun_out(fn, *args, **kwargs):
def fun_inner(*args, **kwargs):
print('Function start execution')
r = fn(*args, **kwargs)
print(r)
print('Function execution is ")
return fun_inner
# Need a function of being decorated
@fun_out # f = fun_out (fun)
def add(a, b):
return a + b
add(1, 2)
operation result """
Function starts
3
The function is executed
import time
def func_out(fn,*argas,**kwargas):
def fun_inner(*args, **kwargs):
print('Function start execution')
a = time.time()
r = fn(*args, **kwargs)
print(r)
b = time.time()
c = round((b-a),3)
print(The 'function is executed, use time% s',c)
return fun_inner
@func_out
def fun(a):
list = []
for i in range(a):
if i % 10 == 0:
list.append(i)
return list
fun(100000)
operation result """
Function starts
[0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 11000, 12000, 13000, 14000, 15000, 16000, 17000, 18000, 19000, 20000, 21000, 22000, 23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000, 31000, 32000, 33000, 34000, 35000, 36000, 37000, 38000, 39000, 40000, 41000, 42000, 43000, 44000, 45000, 46000, 47000, 48000, 49000, 50000, 51000, 52000, 53000, 54000, 55000, 56000, 57000, 58000, 59000, 60000, 61000, 62000, 63000, 64000, 65000, 66000, 67000, 68000, 69000, 70000, 71000, 72000, 73000, 74000, 75000, 76000, 77000, 78000, 79000, 80000, 81000, 82000, 83000, 84000, 85000, 86000, 87000, 88000, 89000, 90000, 91000, 92000, 93000, 94000, 95000, 96000, 97000, 98000, 99000]
The function is executed,time cost%s 0.005
content First, enter functions: input Second, the operator in Python 1. Special operator in Python 2. Special assignment in Python 3. Special comparison operators in ...
2021/2/2 Return value of the function The return value is the result of the function after the function is executed. Specify the return value of the function via return Return can be followed by any o...
Python Getting Started Note (3) - Python Functions and Class General built-in function: ABS (-1) find absolute values, return 1; The maximum value in the MAX ([1, 2, 3]) can be a list or a group; Min ...
In the higher-order functionsProgramming function (a function to write another function)Very common, higher-order functions into two categories: 1, higher-order functionsParameter is a function(As a f...
Recalling the constructor, father, Subclass, Subclass initialization, requires the construction parameters of the subclass. Particotomy function Parent class, display the declaration two constructor. ...
Articles directory 1. Built -in annotations 1.1 The essence of annotation 1.2 yuan annotation 1.3 Note work principle 2. Create annotations 3. View annotation information 4. Note use: Customized seria...
1. Comment 1.1 Single-line comments # This is a statement pycharm shortcut: Ctrl + / More than 1.2-line comments ‘’’ This is a statement ‘’’ or “”"...
function The difference between functions and stored procedures: Stored procedure: There can be zero or multiple returns, suitable for batch insert and batch update. function: There can be only one re...
I recorded the technical video address: Welcome to watch. The Python knowledge points introduced in the previous chapters mainly focus on the environment layout and basic use of Python. Today's chapte...
"Python Basic Tutorial (3rd Edition)" Note: Chapter 10 RE 10.1 module 10.1.1 Module is the program Any Python program can be imported as a module. The storage location of the file is very im...