On Sat, May 19, 2018 at 4:12 PM, Greg Ewing <greg.ew...@canterbury.ac.nz> wrote: > Paul Svensson wrote: > >> I don't quite get what's so subtle about it, am I missing something? >> >> The "with" keyword calls "__enter__", and "as" gives it a name. >> >> Just like "-x + y" is different from "-(x + y)", > > > I think the difference is that mentally one already tends to > think of "with x as y" being grouped "with (x as y)" rather > than "(with x) as y". So, if "x as y" becomes a legal expression > all by itself, it will *seem* as though the meaning of "as" is > changed simply by adding explicit parentheses around an > expression that was already implicitly parenthesised. > > Whether this is too subtle or not is a matter of opinion, but > it was raised as one of the objections to using "as", so some > people obviously thought so.
The reason it's dangerously subtle is that this will work: with (open("filename") as f): And this will work: with ( open("filename") as f ): And this will fail: with (threading.Lock() as lock): Do you know why? Do you want to try to debug that? I can guarantee you that a lot of people will go "hey, cool, now I can use parentheses in 'with' statements to wrap them across lines", and will do so very happily for a long time, wrapping their file-open blocks without an issue. But the semantics have changed *and they will not even know*. The current meaning of "as" is never "bind the expression on the left to the target on the right". Oh and here's another one: Should the target be required to be a name, or should it be allowed to be more complicated (eg "blah blah as self.spam")? Because Python isn't consistent on that point. Do you know which uses of "as" allow other targets than names and which don't? Again, do you want to have to debug situations where parentheses don't seem to matter for simple names, but with another kind of target, they're mandatory / forbidden? (Depending on which way the semantics are defined.) Using "as" in this way will NOT be consistent with the rest of Python, and it will introduce many subtleties for both the compiler and for humans. ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/