TLDR: Can we implement yield or generator instruction (with a loop) within lambda ?
My question is to clarify:
Is it possible to implement the following simple loop function with output
def loopyield(): for x in range(0,15): yield x print(*loopyield())
Error results:
lamyield=lambda x: yield x for x in range(0,15) ^ SyntaxError: invalid syntax
It looks like he was expecting something as the right operand for an unwritten return statement, but found yeild and got confused.
Is there a legal way to achieve this in a loop?
Side note: yield can be an expression / expression depending on who you ask: yield - statement or expression?
The final answer: income can be used with lambda, but the restriction (single line) makes it useless. for/while not possible in lambda because they are not expressions. -user2357112 implicit for the loop is possible with list comprehension, and profitability is valid in list comprehension. -wim
Verdict - Explicit loops are not possible because lambdas in python can only contain expressions, and you need operators to write an explicit loop. -wim
theMobDog
source share