Nick Coghlan wrote: > On 24 March 2018 at 23:29, Christoph Groth <christ...@grothesque.org> wrote: > > x = [n * m for n in range(4) for m in range(5)] > > > > be equally well equivalent to > > > > def <listcomp>(): > > ret = [] > > for n in range(4): > > for m in range(5): > > ret.append(n * m) > > return ret > > x = <listcomp>() > > > > As far as I can tell, with today's Python both implementations (yours > > and mine) are indistinguishable by the user. > > > > They can be distinguished, just not at module or function scope. To give a > concrete example: > > ========== > >>> class C: > ... sequence = range(10) > ... listcomp = [x for x in sequence] > ... def works(data): > ... return list(data) > ... from_works = works(sequence) > ... def fails(): > ... return list(sequence) > ... > >>> C.listcomp > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > >>> C.from_works > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > >>> C.fails() > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "<stdin>", line 8, in fails > NameError: name 'sequence' is not defined > ==========
Thanks a lot for this example. So you mean that while listcomp = [x for x in sequence] works above listcomp = [(y,x) for y in sequence for x in sequence] doesn't (but it would if the class was replaced by a function). That's indeed a bit strange, and I would consider it somewhat of a wart of the language. But as far as I can tell remaining compatible with the above behavior does not force us to leak assignments from the outermost scope of a comprehension. I.e. there's nothing in the language currently that forces listcomp = [x for x in (r := sequence)] to leak the name "r". Granted, it's a bit strange if in the above line the name "sequence" is evaluated in class scope but the name "r" is set in the comprehension scope, but since currently there is no way to assign values to names in comprehensions this "hybrid" behavior would be backwards-compatible, and less surprising than leaking "r". This could be fixed if backwards compatbility may be ever broken again, but until then I don't expect it to be a problem. _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/