On Sun, 19 May 2019 at 12:17, Yonatan Zunger <[email protected]> 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: The construction
>
> with expr1 as var1, expr2 as var2, ...:
> body
>
> fails (with an AttributeError) unless each expression returns a value
> satisfying the context manager protocol. 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 that method is return
> False.
What you describe sounds like the Maybe monad or the Option enum in Rust to me.
with Maybe(expr1) as var1, expr2 as var2, ...:
body
doesn't seem ugly to me, and would make clear whether expr1 was
something that had to have a value or could, maybe, be None.
Whether the code within body would benefit most from Maybe unwrapping
expr1, or from Maybe offering one of the common monad interfaces like
map or iteration, is a related, interesting question.
-Rob
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/