[Python-Dev] (name := expression) doesn't fit the narrative of PEP 20 (original) (raw)
Chris Angelico rosuav at gmail.com
Wed Apr 25 20:20:40 EDT 2018
- Previous message (by thread): [Python-Dev] (name := expression) doesn't fit the narrative of PEP 20
- Next message (by thread): [Python-Dev] (name := expression) doesn't fit the narrative of PEP 20
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Thu, Apr 26, 2018 at 10:11 AM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
Just yesterday this snippet was used on python-dev to show how great the new syntax is:
myfunc(arg, buffer=(buf := [None]*getsize()), size=len(buf)) To my eye this is an anti-pattern. One line of code was saved, but the other line becomes less readable. The fact that 'buf' can be used after that line means that it will be harder for a reader to trace the origin of the variable, as a top-level "buf = " statement would be more visible.
Making 'buf' more visible is ONLY a virtue if it's going to be used elsewhere. Otherwise, the name 'buf' is an implementation detail of the fact that this function wants both a buffer and a size. Should you want to expand this out over more lines, you could do this:
template = [None] buf = template*get_size() length = len(buf) my_func(arg, buffer=buf, size=length)
What are the names 'template' and 'length' achieving? Why should they claim your attention? They are useless relics of a done-and-dusted calculation, being retained for no reason. They do not deserve top-level placement.
The form as given above is starting to get a bit noisy, but I strongly disagree that 'buf' deserves to be a stand-alone name. It is as valueless as 'template' is.
ChrisA
- Previous message (by thread): [Python-Dev] (name := expression) doesn't fit the narrative of PEP 20
- Next message (by thread): [Python-Dev] (name := expression) doesn't fit the narrative of PEP 20
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]