【问题标题】:Multiple lambda functions in zipped list not executing压缩列表中的多个 lambda 函数未执行
【发布时间】:2020-02-13 12:19:32
【问题描述】:

我想在满足条件时执行具有不同参数的多个函数,但我从来没有执行过它们中的任何一个:

conditions = [condition1, condition2, condition3]
functions = [lambda: self._f(), lambda x: self._f2(a), lambda x,y: self._f3(b, c)]

for condition, function in zip(conditions, functions):
    if condition:
        function() # execute the proper function with 0, 1 or 2 arguments
        break

def _f():
    print('function1 with no arguments')

def _f2(x):
    print(f'function2 with one argument {x}')

def _f3(x, y):
    print(f'function2 with two arguments {x} and {y}')

但是当条件满足时,不执行相应的函数。我做错了什么??

提前谢谢你!!

【问题讨论】:

  • break 退出整个for循环
  • 您的第一个 lambda 不会调用 self._f 使用 self._f()
  • 什么是type? `conditions = 行在语法上不是写的有效
  • 通过以上所有注释,您可以简化:condition = lambda x: x in ['YES', 'MAYBE', 'TRY AGAIN'],然后通过condition(type) 调用它。此外,您知道 conditions 在定义时进行评估,之后更改 type 对它没有任何影响
  • 我真正的问题,独立于变量和条件,是如何定义带有可变参数的函数并用唯一的语句()调用它们

标签: python python-3.x list lambda list-comprehension


【解决方案1】:

我希望这会有所帮助:

选项 1:

class MyTestClass:
    def loopTest(self, type, value, value2):
        # necessary to iterate over the values later on
        possibleArgs = [value, value2]

        conditions = [type == 'YES', type == 'MAYBE', type=='TRY AGAIN']
        # here you made the mistake that the first lambda expression didnt call the function "_f" it just "looked" at the reference
        sentences = [lambda: self._f(), lambda x: self._f2(x), lambda x,y: self._f3(x, y)]

        for condition, sentence in zip(conditions, sentences):
            if condition:
                # necessary to pass the arguments to the function
                # "sentence.__code__.co_argcount" gives us the number of arguments of the function
                args = (possibleArgs[i] for i in range(sentence.__code__.co_argcount))
                sentence(*args)
                break

    def _f(self):
        print('executed')

    def _f2(self, x):
        print(f'executed with value {x}')

    def _f3(self, x, y):
        print(f'executed with values {x} and {y}')

MyTestClass().loopTest("TRY AGAIN", 5, 2)
# output: executed with values 5 and 2

选项 2:

class MyTestClass:
    def loopTest(self, type, value, value2):
        a = value
        b = value2

        conditions = [type == 'YES', type == 'MAYBE', type=='TRY AGAIN']
        # here you made the mistake that the first lambda expression didnt call the function "_f" it just "looked" at the reference
        sentences = [lambda: self._f(), lambda: self._f2(a), lambda: self._f3(a, b)]

        for condition, sentence in zip(conditions, sentences):
            if condition:
                sentence()
                break

    def _f(self):
        print('executed')

    def _f2(self, x):
        print(f'executed with value {x}')

    def _f3(self, x, y):
        print(f'executed with values {x} and {y}')

MyTestClass().loopTest("TRY AGAIN", 5, 2)
# output: executed with values 5 and 2

【讨论】:

  • 好的,选项 2 工作正常。我的错误是在 lambda 函数中定义 x 和 y!谢谢!
  • 这个我知道...出错了哈哈
猜你喜欢
  • 2017-11-22
  • 2017-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多