Discuss / Python / 不明白

不明白

Topic source
在此插入代码

def _not_divisible(n): return lambda x: x % n > 0 return返回了什么东西?

葛斯特

#2 Created at ... [Delete] [Delete and Lock User]

其实这个函数是返回了个带有参数n的另一个函数,我写一个例子你自己运行一下就明白了:

def a(x):
    return lambda y: x + y

#调用a(x)
b = a(2)  #返回了另外一个函数b(x),内容为:x+2
#调用b(x)
b(3)
5  # 得到结果5

#直接输入2个参数
a(2)(3)
5 # 得到结果5

绿风雪荧

#3 Created at ... [Delete] [Delete and Lock User]

def primes(): yield 2 it = _odd_iter() # 初始序列 while True: n = next(it) # 返回序列的第一个数 yield n it = filter(_not_divisible(n), it) # 构造新序列 构造新序列那句: filter里面_not_divisible函数为什么要带(n),为什么不能省去?

灵魂_挽歌

#4 Created at ... [Delete] [Delete and Lock User]
def a(x):
    return lambda y: x+y
#调用a(x)
b = a(2)    #返回另一个函数y,内容为:2 + y
b(3)    #y=3,返回5(2+3)
5

  • 1

Reply