On Mon, Apr 23, 2018 at 6:22 AM, Mike Miller <python-...@mgmiller.net> wrote: > On 2018-04-22 12:37, Chris Angelico wrote: >> Kinda, except that that's not quite a match either. But mainly, the >> comparison with 'with' and 'except' is dangerously incompatible. > > Hmm, looks very close conceptually, though mechanics are different. > > Dangerous feels like an exaggeration however. I've made the argument that > occurrences would be very rare, but if I'm wrong, the code should blow up on > its first run. Perhaps a sanity check could be put in?
with open(fn) as f: with (open(fn) as f): These two do the same thing, but only because a file object's __enter__ returns self. So it's dangerous, because it WILL work... and people will get into the habit of parenthesizing to permit a 'with' statement to go across line breaks. And then they'll use a different context manager, like closing(), or a PsycoPG2 database connection (I think), where it returns something else. And it'll work, until they go over multiple lines, and then suddenly the semantics change. It's as bad as writing JavaScript code like this: function f(x) { return x + 1; } and then transforming it to this: function f(x) { return x + 1; } and having it change in behaviour. (Yes, it happens. Welcome to JavaScript, where implicit semicolons are a thing.) >> Intuitive consistency isn't enough to handle complex cases. >> Programming languages that favour intuitive consistency end up with a >> million special cases. > > Ok, but I think we have all the tools we need here, there's just an extra > place to stub your toe out in the weeds. > > To turn the question around, are we really worried that this awkward code > (or some variant) is about to be written? > > with (cm_obj := callable()) as enter_result_obj: > cm_obj.write() # AttributeError > > If not, I argue it is a theoretical problem that, if hit, blows up > immediately. Were it to blow up immediately, I wouldn't be too bothered. ChrisA _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com