【问题标题】:Please help me understand this python code line by line. I'm facing problem in understanding the flow of data in this code请帮我逐行理解这个python代码。我在理解这段代码中的数据流时遇到了问题
【发布时间】:2021-06-21 20:17:12
【问题描述】:
def myfunc(z):
    return lambda x:x+1
t=myfunc(1)
print(t(2))

#我在理解这段代码中的数据流时遇到了问题。

【问题讨论】:

  • myfunc 返回一个函数。该函数将 1 添加到它传递的任何内容。分配后,就像你说的def t(x): / return x+1

标签: python-3.x


【解决方案1】:

简单的解释:

你的定义:

def myfunc(z):
    return lambda x:x+1
t=myfunc(1)
print(t(2))

与(因为您不在任何地方使用 z)相同:

def myfunc():
    return lambda x:x+1

t=myfunc()
print(t(2))

相当于:

t = lambda x:x+1
print(t(2))

相当于:

def t(x):
   return x+1

print(t(2))

它的作用是将myfunc 的结果分配给变量t,这是一个lambda(另一个函数定义)

然后你执行t 参数x 设置为2 计算结果为 2+1 并返回 3

【讨论】:

    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 2013-06-15
    • 2011-02-21
    相关资源
    最近更新 更多