Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: lambda, yield-statement/expression and loops (Clarify)

TL;DR: Can we implement yield or generator statement (with a loop) within a lambda?

My question is to clarify:

Whether the following simple loop function can be implemented with yield

def loopyield():    for x in range(0,15):       yield x print(*loopyield()) 

Results in error:

lamyield=lambda x: yield x for x in range(0,15)                        ^ SyntaxError: invalid syntax 

Which looks like, it was expecting something as right operand for unwritten return statement but found the yield and getting confused.

Is there a proper legit way to achieve this in a loop?

Side note: yield can be statement/expression depending on who you ask: yield - statement or expression?

Final Answer : yield can be used with lambda but the limitation(single-line) makes it useless. for/while not possible in lambda because they are not expressions. -user2357112 implicit for loop is possible with list comprehension, and yield is valid within the list comprehension. -wim

Verdict- Explicit loops not possible because lambdas in python can only contain expressions, and to write an explicit loop you will need to use statements. -wim

like image 272
theMobDog Avatar asked Nov 14 '16 17:11

theMobDog


People also ask

Can a lambda yield?

While you can technically put a yield in a lambda function, the constraints of lambda functions make it essentially never a useful thing to do.

Can we use for loop in lambda expression Python?

Since a for loop is a statement (as is print , in Python 2. x), you cannot include it in a lambda expression.

How does Python define lambda expressions?

In Python, a lambda function is a single-line function declared with no name, which can have any number of arguments, but it can only have one expression. Such a function is capable of behaving similarly to a regular function declared using the Python's def keyword.

What is the correct syntax for lambda expression in Python?

Syntax. Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).


1 Answers

The one-liner you seem to be trying to create is actually technically possible with a lambda, you just need to help the parser a bit more:

>>> lamyield = lambda: [(yield x) for x in range(15)] >>> print(*lamyield()) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

This uses a for loop implicitly in a list comprehension. It is not possible with an explicit while loop or for loop outside of a comprehension. That's because lambdas in python can only contain expressions, and to write an explicit loop you will need to use statements.

Note: this syntax is deprecated in Python 3.7, and will raise SyntaxError in Python 3.8

like image 116
wim Avatar answered Sep 28 '22 01:09

wim