On 8 March 2016 at 22:21, Sjoerd Job Postmus email@example.com wrote:
As for yet another syntax suggestion (if we want to introduce something).
[y for x in numbers with abs(x) as y if y > 5]
The benefit is that it reads quite natural: "with expr as name". Another benefit is that keywords get reused.
Agreed, this is a plausible suggestion. But rather than just providing an example usage, it would be helpful to see the full syntax of comprehension with the proposed addition. See https://docs.python.org/3/reference/expressions.html#grammar-token-comprehen... - note that a comprehension can have arbitrarily many for and if clauses, interspersed in any order as long as the first one is a "for". I'm guessing you'd add "with <some_sort_of_expression> as <target_list>" as simply a third option. But what would the semantics be? I'm guessing that "with xxx as yyy" translates basically as a statement "yyy = xxx" in the notional expansion described in that section ("considering each of the for or if clauses a block...")
Assuming that is the proposed definition, a few questions arise:
1. Is the behaviour this would assign to "with <foo> as x with <bar> as x" (i.e., repeated bindings of the same name) what we'd want? Is it likely to cause confusion in practice? 2. Do we need to restrict the <target_list>? Consider that "with something as global_var" is allowed by the definition - that would leak values out of the comprehension. This isn't new - the "for" variable works like this already - but is it something that's more likely to be abused than currently? Should we care? (Python doesn't tend to prohibit people from writing bad code).
However, that already has semantics outside a comprehension for context-managers.
This is indeed a concern, albeit not a huge one - the definition has to point out that in the expansion "with" should be read as an assignment (written backwards) not as a with statement. It's not an impossible burden, but it is mildly inconsistent.
This is probably the syntax I prefer of the ones suggested so far. But I still haven't seen any *really* convincing arguments that we need anything new in the first place.