Re: [Python-Dev] Generator objects and list comprehensions?
Craig Rodrigues <rodrigc@freebsd.org>:
Make this return a list on Python 3, like in Python 2: [(yield 1) for x in range(10)]
Give Python 2 a little more credit. Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
[(yield 1) for x in range(10)] File "<stdin>", line 1 SyntaxError: 'yield' outside function
regards, Anders
On Feb 2, 2017, at 2:17 AM, Anders Munch <ajm@flonidan.dk> wrote:
Give Python 2 a little more credit.
We are, it told you what your issue was: yield outside a function. Consider:
def f(): ... l = [(yield 1) for x in range(10)] ... print(l) gen = f() for i in range(11): ... gen.send(i or None) ... 1 1 1 1 1 1 1 1 1 1 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
But this is a very convoluted "feature" and likely people don't expect *this* to be what's happening. - Ł
participants (2)
-
Anders Munch -
Lukasz Langa