Advanced a python (Functional Programming) as a function of the python [2-1 Parameters

2-1 python to function as a parameter

1 def add(x, y, f):
2     return f(x) + f(y)

If passed as a parameter f abs values:

1 add(-5, 9, abs)

The function definition, function code is actually executed:

1 abs(-5) + abs(9)

Since the parameters x, y and f are arbitrary pass, if passed to other functions f, it can be a different return value.

task

Using add (x, y, f) function is calculated:

1 import math
2     
3 def add(x , y , f):
4     return f(x) + f(y)
5 
6 print add(25,9,math.sqrt)

 

Guess you like

Origin www.cnblogs.com/ucasljq/p/11608682.html