In mathematics and Computer Science , Higher order function (High-order Function) Functions that should satisfy at least one of the following conditions :
def counter(base):
def _counter(step=1):
nonlocal base
base += step
return base
return _counter
# The base number is 0, In steps of 2 Technical device for
counter1 = counter(base=0)
for i in range(3):
print(counter1(step=2))
# Out
2
4
6
# == If you can't compare content , It is equivalent to is
# for example Function object Unable to compare content Just compare the memory addresses directly
def counter(base):
def _counter(step=1):
nonlocal base
base += step
return base
return _counter
f1 = counter(5)
f2 = counter(5)
print('f1 = {}, f2 = {}'.format(f1, f2))
print('f1_id = {}, f2_id = {}'.format(id(f1), id(f2)))
print('f1() = {}, f2() = {}'.format(f1(), f2()))
print('f1()_id = {}, f2()_id = {}'.format(id(f1()), id(f2())))
print(f1 == f2)
print(f1() == f2())
f1 = <function counter.<locals>._counter at 0x00000000078B13A8>, f2 = <function counter.<locals>._counter at 0x00000000078B1558>
f1_id = 126555048, f2_id = 126555480
f1() = 6, f2() = 6
f1()_id = 8791210553808, f2()_id = 8791210553808
False
True
# == If you can't compare content , It is equivalent to is
# for example Function object Unable to compare content Just compare the memory addresses directly
# Globally defined functions , It is equivalent to a global variable , No matter how many variables are assigned , Its id It's all the same
inc_num = 100
def inc(step=1):
return inc_num+1
def counter(base=0):
print(id(inc))
return inc
print(1, inc, id(inc), id(inc()))
counter()
print(2, inc, id(inc), id(inc()))
f1 = counter(5)
f2 = counter(7)
print('f1 = {}, f2 = {}'.format(f1, f2))
print('f1_id = {}, f2_id = {}'.format(id(f1), id(f2)))
print('f1() = {}, f2() = {}'.format(f1(), f2()))
print('f1()_id = {}, f2()_id = {}'.format(id(f1()), id(f2())))
print(f1 == f2)
print(f1() == f2())
1 <function inc at 0x0000000008032C18> 134425624 8791210556816
134425624
2 <function inc at 0x0000000008032C18> 134425624 8791210556816
134425624
134425624
f1 = <function inc at 0x0000000008032C18>, f2 = <function inc at 0x0000000008032C18>
f1_id = 134425624, f2_id = 134425624
f1() = 101, f2() = 101
f1()_id = 8791210556816, f2()_id = 8791210556816
True
True
sorted(iterable, /, *, key=None, reverse=False)
sorted
The function returns a new list list.sort()
( Modify in place , The return is None) The difference between sorted function And list sort Method
The difference between
list1 = [1, 2, 3]
list2 = sorted(list1, key=lambda x:6-x) # Does not affect the original list , Return to the new list
print(list1, list2)
# [1, 2, 3] [3, 2, 1]
list1 = [1, 2, 3]
list2 = list1.sort(key=lambda x:6-x) # Modify in place
print(list1, list2)
# [3, 2, 1] None
sorted function
list3 = [1, 2, 3, 'a', 'b', 'A']
list4 = sorted(list3, key=str)
list5 = sorted(list3, key=str, reverse=True)
for i in [list3, list4, list5]:
print(i)
for j in list3:
print(ord(str(j)), end=' |=| ')
[1, 2, 3, 'a', 'b', 'A']
[1, 2, 3, 'A', 'a', 'b']
['b', 'a', 'A', 3, 2, 1]
49 |=| 50 |=| 51 |=| 97 |=| 98 |=| 65 |=|
# ord() Built in function help
Signature: ord(c, /)
Docstring: Return the Unicode code point for a one-character string.
Type: builtin_function_or_method
filter(self, /, *args, **kwargs)
filter(function or None, iterable) --> filter object
function
A parameter is a function of a parameter , And the return value should be bool
type , Or its return value is equivalent to Boolean value function
If the parameter is None
, Each element of an iteratable object has its own Boolean equivalent # Filter out the list that cannot be 3 It can't be divided 2 The value of the division
iter1 = filter(lambda x:x%3!=0 and x%2!=0, list(range(10)))
# Returns an iterator
print(iter1, type(iter1))
print(list(iter1))
<filter object at 0x0000000007B02E88> <class 'filter'>
[1, 5, 7]
# function If the parameter is `None`, Each element of an iteratable object has its own Boolean equivalent
print(list(filter(None, range(5))))
print(list(filter(None, range(-5, 5))))
print(list(filter(None, [0, False, True, [], [1], {
}, {
1}])))
[1, 2, 3, 4]
[-5, -4, -3, -2, -1, 1, 2, 3, 4]
[True, [1], {
1}]
# if fn(element): yield element
print(list(filter(lambda x:True, range(5))))
print(list(filter(lambda x:False, range(5))))
print(list(filter(lambda x:None, range(5))))
[0, 1, 2, 3, 4]
[]
[]
map(self, /, *args, **kwargs)
map(func, *iterables) --> map object
iter2 = map(lambda x:x+1, range(5))
print(iter2, type(iter2))
print(list(iter2))
<map object at 0x0000000007B295C8> <class 'map'>
[1, 2, 3, 4, 5]
print(list((map(lambda x:x**2, range(1,6)))))
print(dict((map(lambda x:(x, x**2), range(1,6)))))
print(dict((map(lambda x, y:(x, y**2), 'abcde', range(1,4)))))
[1, 4, 9, 16, 25]
{
1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
{
'a': 1, 'b': 4, 'c': 9}
# set Sets are unordered
dict(map(lambda x,y:{
x, y}, 'abcde', range(10)))
# {0: 'a', 1: 'b', 2: 'c', 3: 'd', 'e': 4}
z=f(x, y)
To z=f(x)(y)
def add(x, y):
return x + y
add(4, 5)
# 9
def add(x):
def _add(y):
return x + y
return _add
add(4)(5)
# 9
def add(x, y, z):
return x + y + z
add(4, 5, 6)
# 15
def add(x):
def _add(y):
def _add1(z):
return x + y + z
return _add1
return _add
add(4)(5)(6)
# 15