The syntax you propose is already in the Alternate syntax and there is an implementation at https://github.com/thektulu/cpython/tree/where-expr
Already discussed, but no conclusion, for me I see two different proposals, the "[y for x in range(5) with y = x+1]" in comprehensions list, and the case in any expression "print (y with y = x+1)", as discussed the second case corresponds to a simple assignment so is less useful, but if the scope is local to the expression, that'd be useful : print(y with y = x+1); print(y) # NameError But I like it :) Le 3 mars 2018 02:36, "Greg Ewing" <greg.ew...@canterbury.ac.nz> a écrit : > PEP 572 as it stands seems to have many problems: > > * Asymmetry between the first and subsequent > uses of the bound value > * Embedded as-clauses are subtle and hard to spot > * Ever more convoluted semantics are being invented > to address percieved pitfalls > > Alternative proposal > -------------------- > > Getting back to the original motivating use case of > intermediate values in comprehensions, to my mind there > is really only one clear and obvious way to write such > things: > > [(f(y), g(y)) for x in things where y = h(x)] > > Possible objections to this: > > * Requires a new keyword, which may break existing code. > > - Yes, but "where" is an unlikely choice of name, being > neither a noun, verb or adjective, so it probably wouldn't > break very *much* code. In return, we get something that > resonates with language mathematicians have been using > for centuries, and a word that can be usefully googled. > > * Out-of-order evaluation > > - Comprehensions and if-expressions already have that. > > Extension to other use cases > ---------------------------- > > There are some other situations in which it could be > useful to have a similar construct. > > 1. Name bindings local to an expression: > > roots = ([(-b-r)/(2*a), (-b+r)/(2*a)] where r = sqrt(b*b-4*a*c)) > > 2. Names bound as part of an expression and also available > in a following suite: > > if m where m = pattern.match(text): > do_something_with(m) > > There's a problem, though -- if "where" in an expression > creates bindings local to that expression, they're not > going to be visible in the rest of a comprehension or > compound statement. > > So I'm thinking that there would actually be three distinct > usages of "where": > > A. As a clause in a comprehension, where it creates bindings > local to the comprehension and is treated on the same footing > syntactically as "for" and "if" clauses. > > B. In an expression, surrounded by parentheses for > disambiguation. Bindings are visible only within the > parentheses. > > C. Following the expression of an "if" or "while" statement. > Bindings are visible within the preceding expression and the > following suite. > > Note that case B avoids the issue of whether expression-local > bindings affect the LHS of an assignment, because in > > a[x] = (x * 2 where x = 17) > > the "x" bound by the where-clause is clearly restricted to > the part in parentheses. This would be a syntax error: > > a[x] = x * 2 where x = 17 # illegal > > -- > Greg > > > > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/