Functions Notes
- Shaped as def (A: stri, B: dict) -> str
pass
Parameters and return values of the function that makes the code to explain better readability
Higher-order functions:
Can accept the function as a parameter, or function as a result of the return of function is called higher-order functions c j y have a common mapping fliter y and reduce
(Understand) in the current version of python j z With some of the features list comprehensions achieved sufficient to achieve more elegant than the higher-order functions
operator package:
Arithmetic operators sometimes need to be used as a function to make certain functions l more elegant
Conventional implementation factorial function:
def fact(n):
return 1 if n < 2 else n*fact(n-1)
With operator of mul
from functools import reduce
from operator import mul
def fact(n):
return reduce(mul,range(1,n+1))
Further there itemgetter operator function of k can be used to implement certain functions defined for the lambda kkey when y is more simple and elegant g
from operator import itemgetter
list_data = [( "Joe Smith", 1), ( "John Doe", 3), ( "Wang Wu", 2)]
for item in sorted(list_data,key = itemgetter(1))
print(item)
The above wording than the use of anonymous functions s kkey = lambda item: item [1] y too obvious advantage
High frequency of use higher-order functions: any, all, sum, sorted, min, max, are interested can learn functools.partial (used to bind a variable to freeze more appropriate)