10. The road of python precipitation--first understanding of higher-order functions

1. Higher-order functions: There are two types: one is that the return value contains the function body; the other is that a function body is passed as a parameter to another function

1. The return value contains the function body

Example 1,

1  def test():
 2      print ( ' This is a test ' )
 3      return test
 4  
5 f = test()
 6 f()
1  this is a test
 2 this is a test

Example 2

1 def inward():
2     print('from inward')
3 def outward():
4     print('from outward')
5     return inward
6 
7 f = outward()
8 f()
1 from outward
2 from inward

2. Pass a function body as a parameter to another function

Example 1

1  def inward(name):
 2      print ( ' %s is from inward ' % name)
 3  
4  def outward(n):
 5      print ( ' I am from outside the earth ' )
 6  
7 outward(inward( ' me ' ))
1 I am from  inward 2
 I am from beyond the earth

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324999024&siteId=291194637