[Python-ideas] With expressions
Benedikt Werner
1benediktwerner at gmail.com
Fri Aug 3 18:40:13 EDT 2018
> For instance, what would the following do?
>
> initial = person.name <http://person.name>[0] with
> suppress(AttributeError) # Hangover from PEP 505 discussion...
As the with-expression mimics the with-statement I would say this is
similar to:
with supress(AttributeError):
tmp = person.name[0]
initial = tmp # Error on assignment wouldn't get suppressed. Not relevant for this case but still.
I don't understand where this is ambigous?
> So maybe it only makes sense to use expression assignment (PEP 572):
>
> data = (d := file.read() with open(...) as file)
>
> To which I say, "Eww..."
I definitely agree that this looks a bit bad but I don't get why you
would consider using an expression assignment there.
data = file.read with open(...) as file
works perfectly fine so why would you want to additonaly assign it to
another variable "d"?
Overall I like the idea of the with-expression as it allows you to make
some common use cases like the open/read example more readable. It's
clear at first sight that what is actually done is reading the file.
While it's true that it's usually easy to write a simple function to do
the job that still isn't as simple to understand and in most cases when
reading the code you then have to look at the function to see if
anything else is done there. Also what if you then do readlines
somewhere? Then you need another function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180804/886a2dca/attachment-0001.html>
More information about the Python-ideas
mailing list