On Feb 8, 2020, at 13:25, Chris Angelico <ros...@gmail.com> wrote:
> 
> On Sun, Feb 9, 2020 at 8:04 AM Random832 <random...@fastmail.com> wrote:
>> 
>>> On Fri, Feb 7, 2020, at 13:03, Todd wrote:
>>> approaches like opening within the dump or load, which the wiki
>>> still recommends [1].
>>> 
>>> So something like:
>>> 
>>> with open('myfile.p', 'wb') as f:
>>> pickle.dump(myobj, f)
>>> 
>>> Would be:
>>> 
>>> pickle.dump(myobj, 'myfile.p')
>> 
>> 
>> What if you could write pickle.dump(myobj, with open('myfile.p', 'wb'))?
>> 
>> Or other similar examples such as (with open('myfile')).read() - have the 
>> compiler automatically transform the code into equivalent to wrapping the 
>> statement in a with block.
> 
> Exactly how much code would be wrapped in the 'with' block?

I think the answer there is pretty obvious: it can only be the entire statement 
that the with expression is contained in.

That does raise an issue for lambdas and comprehensions. I think for that case, 
you need to stop thinking of it as a syntactic transformation and instead think 
of the semantics directly: the with affects the innermost scope if it’s smaller 
than the innermost statement, even though there’s no way to rewrite it like 
that. But then nothing else in Python is defined as a syntactic rewrite, so I 
don’t think that’s a problem.

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/C4AEQU3R3DQG3GQAL6MUQLB2KU6XRN6P/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to