Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Robert Collins
On Sun, 19 May 2019 at 12:17, Yonatan Zunger wrote: > > Hi everyone, > > I'd like to bounce this proposal off everyone and see if it's worth > formulating as a PEP. I haven't found any prior discussion of it, but as we > all know, searches can easily miss things, so if this is old hat please LMK

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Steven D'Aprano
On Sat, May 18, 2019 at 07:44:26PM -0700, Yonatan Zunger wrote: > Essentially, a "noop with" improves code health, I don't think I can accept that as a mere assertion. I'd like to see some concrete evidence that it does. > > > For example, this is a good pattern: > > > > > > with functionRetur

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Yonatan Zunger
Terry, let me make sure I'm understanding your responses. (1) Only certain things should be CM's, and those things should be explicitly denoted as such. (2) > When a function returns something useful or None, I think an immediate > test is generally good practice. It will usually make the code

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Yonatan Zunger
I would definitely love that kind of subscoping syntax, but as you say, that would be a much larger change. :) The use of this for things like '2+2' would be, as you say, syntactic sugar; the compiler could even be clever and strip it out of the bytecode entirely. Its only purpose in that context

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Chris Angelico
On Sun, May 19, 2019 at 11:46 AM David Mertz wrote: >> >> I think you probably mean something other than what you actually write. It >> doesn't really make sense for "any expression" as far as I can tell. What >> would it possibly mean to write: >> >> with (2+2) as foo: >> print(foo) > > >

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread David Mertz
Oh, I neglected to include the definition of my boring Foo class: >>> class Foo(object): ... def __init__(self, val): ... self.val = val ... The only important thing about it is that it DOES NOT have __enter__() or __exit__() methods. On Sat, May 18, 2019 at 9:40 PM David Mertz wrot

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread David Mertz
> > I think you probably mean something other than what you actually write. > It doesn't really make sense for "any expression" as far as I can tell. > What would it possibly mean to write: > > with (2+2) as foo: > print(foo) > I have occasionally thought it would be nice to do something like

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread David Mertz
On Sat, May 18, 2019 at 8:17 PM Yonatan Zunger wrote: > Instead, we should permit any expression to be used. If a value does not > expose an __enter__ method, it should behave as though its __enter__ > method is return self; if it does not have an __exit__ method, it should > behave as though tha

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Terry Reedy
On 5/18/2019 8:13 PM, Yonatan Zunger wrote: Hi everyone, I'd like to bounce this proposal off everyone and see if it's worth formulating as a PEP. I haven't found any prior discussion of it, but as we all know, searches can easily miss things, so if this is old hat please LMK. *Summary: *Th

Re: [Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Batuhan Taskaya
Good idea, +1 from me. On Sun, May 19, 2019, 3:17 AM Yonatan Zunger wrote: > Hi everyone, > > I'd like to bounce this proposal off everyone and see if it's worth > formulating as a PEP. I haven't found any prior discussion of it, but as we > all know, searches can easily miss things, so if this

[Python-ideas] Proposal: Allowing any variable to be used in a 'with... as...' expression

2019-05-18 Thread Yonatan Zunger
Hi everyone, I'd like to bounce this proposal off everyone and see if it's worth formulating as a PEP. I haven't found any prior discussion of it, but as we all know, searches can easily miss things, so if this is old hat please LMK. *Summary: *The construction with expr1 as var1, expr2 as var2,