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

David Mertz mertz at gnosis.cx
Sat May 18 21:45:16 EDT 2019


>
> 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 this (and
I could, but I haven't, so I guess I don't think it that strongly):

>>> @contextmanager
... def bind(val):
...     yield val
...
>>> with bind(2+2) as four:
...     print(four)
...
4

-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190518/79f4b783/attachment.html>


More information about the Python-ideas mailing list