Lambda自我做什么: [英] What does the lambda self: do

查看:97
本文介绍了Lambda自我做什么:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案说明了如何动态创建测试用例.

This answer explains how to create test cases dynamically.

答案的代码:

class Tests(unittest.TestCase):
    def check(self, i, j):
        self.assertNotEquals(0, i-j)

for i in xrange(1, 4):
    for j in xrange(2, 6):
        def ch(i, j):
            return lambda self: self.check(i, j)
        setattr(Tests, "test_%r_%r" % (i, j), ch(i, j))

我已经测试过并且可以正常工作,但是我不能仅仅弄清楚如何?

I've tested and it works, but I can't just figure out how?

我在这里很难理解lambda self:魔术,主要是:

I have trouble understanding the lambda self: magic in play here, mainly:

  1. 这里使用的lambda用来执行与functools.partial()完全相反的操作(即使用一个未知的 extra 参数创建包装函数)
  2. self是有意义的关键字还是lambda spam同样有效?
  3. lambda评估了什么?
  4. class es范围之外,.check()为何完美?
  5. 如何在没有 lambda的情况下执行此操作? -如果我理解正确,您应该可以在没有lambda 的情况下进行操作(更不用说我同意Guido和Alex 的眼睛,我不要:)
  1. Is the lambda used here to perform the exact opposite of functools.partial() (i.e. to create a wrapper function with one extra parameter that is not yet known)
  2. Is self a meaningful keyword or would lambda spam would work just as well?
  3. What point is that lambda evaluated?
  4. How come the .check() is perfectly fine outside the classes scope?
  5. How do you do this without lambda? - if I understand correctly, you should be able to do without lambda (not to mention that I agree with Guido and Alex that it is an eyesore and I want to do without :)

推荐答案

首先:函数返回lambda以防止稍后使用ij的修改值.

First of all: The lambda is returned from a function to prevent that modified values of i and j will be used later.

与以下内容比较:

for i in xrange(1, 4):
    for j in xrange(2, 6):
        setattr(Tests, "test_%r_%r" % (i, j), lambda self: self.check(i, j))

这将无法正常工作,因为所有lambda都共享相同的名称空间,并且在循环结束后将使用ij的值.如果我们使用单独的函数调用,则每次引入一个新的闭包,都会在调用函数的时间点捕获ij的值.

This will not work as expected, as all the lambdas will share the same namespace and will use the values of i and j after the end of the loop. If we use a separate function call, we introduce a new closure every time that captures the values of i and j at the point in time where the function is called.

我们可以通过将ij的当前值保存为lambda的默认参数来实现相同的目的.为了达到良好的效果,我们还使用itertools.product来避免嵌套循环:

We could achieve the same by saving the current values of i and j as default arguments of the lambda. For good measure, let's also use itertools.product to avoid the nested loop:

from itertools import product
from operator import methodcaller

for i, j in product(range(1, 4), range(2, 6)):
  setattr(Tests, "test_%r_%r" % (i, j),
          lambda self, i=i, j=j: self.check(i, j))

这里使用的lambda函数执行的功能与functools.partial()完全相反(即使用一个尚不知道的额外参数创建包装函数)

Is the lambda used here to perform the exact opposite of functools.partial() (i.e. to create a wrapper function with one extra parameter that is not yet known)

并非如此.它只是对作为参数给出的check(i, j)方法进行调用.此处用于动态地将方法添加到Tests类.

Not really. It just calls the check(i, j) method on whatever it is given as an argument. This is used here to dynamically add methods to the Tests class.

自我是一个有意义的关键字,还是lambda垃圾邮件也会同样起作用?

Is self a meaningful keyword or would lambda spam would work just as well?

spam也可以正常工作.由于约定,他们在这里选择self,因为lambda表示方法.

spam would work just as well. They choose self here due to convention because the lambda represents a method.

lambda评估了什么?

What point is that lambda evaluated?

一旦在Tests的实例上调用test_[i]_[j]().

.check()在类范围之外为什么很好?

How come the .check() is perfectly fine outside the classes scope?

因为它在lambda中,所以以后只会使用Tests的实例作为self参数来调用.

Because it's inside a lambda will only be called later with an instance of Tests as the self argument.

这篇关于Lambda自我做什么:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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