0

I've found some other questions that deal with the problem of creating lambdas inside loops, and the last value in the iteration being assigned to all lambda functions. While the solution is generally using lambda x=x: instead of lambda x:, or using functools.partial, what should I do when the lambda expects *args?

>>> keys = ['a', 'b', 'c']
>>> for func in [lambda *args: print(x, args) for x in keys]:
...     func(1, 2, 3)
... 
c (1, 2, 3)
c (1, 2, 3)
c (1, 2, 3)

Expected result:

a (1, 2, 3)
b (1, 2, 3)
c (1, 2, 3)

The actual code is:

for event in emitter_events:
    self.ws.on(event, lambda *args: logging.debug(f"'{event}': {repr(args)}"))
admirabilis
  • 2,290
  • 2
  • 18
  • 33

0 Answers0