当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Black lambda Magic

问题描述:

In scipy.splev I found this piece of code:

def splev( x, tck, int der=0, int ext=0):

t,c,k = tck

try:

c[0][0]

parametric = True

except:

parametric = False

if parametric:

return list(map(lambda c, x=x, t=t, k=k, der=der: splev(x, [t,c,k], der, ext), c ))

else:

.

.

.

return y.reshape(shape)

and I fail to understand the lambda, please enlighten me.

网友答案:

The lambda function here:

lambda c, x=x, t=t, k=k, der=der: splev(x, [t,c,k], der, ext)

is the same as:

def anonymous_lambda_function(c, x=x, t=t, k=k, der=der):
    return splev(x, [t,c,k], der, ext)

and map is mapping this function to the elements of an iterable, c.

The list(map(...)) is redundant in Python 2, but it's necessary in Python 3, so it preserves the code's forward compatibility.

网友答案:

lambdas are just unnamed functions. You could write it like this:

def splev( x, tck, int der=0, int ext=0):

    t,c,k = tck

    def otherfxn(c, x=x, t=t, k=k, der=der):
        return splev(x, [t,c,k], der, ext)

    try:
        c[0][0]
        parametric = True
    except:
        parametric = False
    if parametric:
        return list(map(otherfxn, c))
    else:
        .
        .
        .
    return y.reshape(shape)
分享给朋友:
您可能感兴趣的文章:
随机阅读: