Python表达式,评估foo吧 [英] Python Expressions , evaluate foo bar

查看:158
本文介绍了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 function foo with parameter x, inside of foo is another function called bar, which also has a (separate) parameter x. When you call foo(3), it returns bar(x * 2), which in this case is equivalent to bar(6), which in turn returns x + 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆