[英]Need good explanation of this small python code
I am actually following python on solo learn and came across this little code.我实际上是在单独学习时关注 python 并遇到了这个小代码。 Need a good explanation of this one.
需要一个很好的解释。 Thank you.
谢谢你。
def add(x, y):
return x + y
def do_twice(func, x, y):
return func(func(x, y), func(x, y))
a = 5
b = 10
print(do_twice(add, a, b))
do_twice
get a function and two parameters. do_twice
得到一个 function 和两个参数。
then return below result:然后返回以下结果:
func(func(param), func(param))
In this specific example:在这个具体的例子中:
add(add(5,10), add(5,10)) = add(15, 15) = 30
do_twice(add, a, b))
add(add(x, y), add(x, y))
this function will return (x+y)*2这个 function 将返回 (x+y)*2
param 1 (func) => add(num1, num2)
param 2 (x) => a number
param 3 (y) => another number
num1 will be x+y num1 将是 x+y
num2 will be x+y num2 将是 x+y
so the value of do_twice will be ((x+y)+(x+y)).所以 do_twice 的值将是 ((x+y)+(x+y))。
I'm improving my English, be patient.我正在提高我的英语,请耐心等待。 I hope that explanation help's you
我希望解释对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.