Python表达式,评估foo吧 [英] Python Expressions , evaluate foo bar
本文介绍了Python表达式,评估foo吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
def foo(x):
def bar(x):
返回x + 1
返回栏(x * 2)
foo(3)
无法理解这个一个..
def foo(x):
def bar(x):
return x + 1
return bar(x * 2)
foo(3)
cannot get my head around this one..
推荐答案
这很简单:你用参数<$ c定义一个函数foo
$ c> x ,在foo
里面是另一个名为bar
的函数,它也有一个(单独的)参数x
。当你打电话给foo(3)
时,它会返回bar(x * 2)
,在这种情况下相当于bar(6)
,它又返回x + 1
,或者在这种情况下,6 + 1,等于7.语法有点奇怪,因为大多数常见语言不允许在其他函数内部使用正确的函数(与lambdas和匿名函数相对),更糟糕的是因为它们具有相同的参数名称。如果他们有不同的参数名称可能会更清楚一点,例如:
It''s fairly simple: you define a functionfoo
with parameterx
, inside offoo
is another function calledbar
, which also has a (separate) parameterx
. When you callfoo(3)
, it returnsbar(x * 2)
, which in this case is equivalent tobar(6)
, which in turn returnsx + 1
, or in this case, 6 + 1, which equals 7. The syntax is a little weird, because most common languages do not allow proper functions (as opposed to lambdas and anonymous functions) inside of other functions, and even worse since both have the same parameter name. It might be a little clearer if they had different parameter names, like:
def foo(x):
def bar(y):
return y + 1
return bar(x * 2)
foo(3)
这篇关于Python表达式,评估foo吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文