4 ms·
It is definitely useful, but it severely reduces readability in my opinion. Your example is also a great example for this. The first version in way more readabl
by muhbags 8y ago
It is definitely useful, but it severely reduces readability in my opinion. Your example is also a great example for this. The first version in way more readable than the second version with the walrus operator.
- gonational 8y agoI agree with you. It's very exciting to remove these sorts of redundant lines, but I cannot train my brain to intuitively view that line as it will be interpreted. There is one case where the benefit, IMHO, far outweighs the negatives, and that can be seen in slides 30-31 of Dustin Ingram's slideshow[1]. PEP 572... the day Python jumped the walrus. 1. https://speakerdeck.com/di_codes/pep-572-the-walrus-operator?slide=30 https://speakerdeck.com/di_codes/pep-572-the-walrus-operator...
- ssutch3 8y agoYes, you can. How do you think your brain learned the original way?
- gonational 8y agoIntuition is defined as being based on what one feels to be true even without conscious reasoning. Something that is unnatural, such as an operator mixed with an expression is anything but natural, in the context of Python. I can force myself to recognize the pattern; yes. Also, the example I linked to in the slide show does feel somewhat intuitive (usage in a while loop).
- a1369209993 8y ago> 1. https://speakerdeck.com/di_codes/pep-572-the-walrus-operator?slide=30 https://speakerdeck.com/di_codes/pep-572-the-walrus-operator... Direct link: https://speakerd.s3.amazonaws.com/presentations/025e2c4bf55143678645a1f3a712e7d2/preview_slide_30.jpg https://speakerd.s3.amazonaws.com/presentations/025e2c4bf551... (s/preview_// for a larger version)
- legostormtroopr 8y agoI can't take a programming talk seriously if it declares in giant font "Less lines are better" (slide 38). Any C program can be written as a single line, with no linebreaks - that doesn't make it "better" by any metric. Python is built around its readability, and slide 44: > group = match.group(1) if (match := re.match(data)) else None is anything but. 'match' is assigned after its first use, and it took me far to long to mentally parse what it was doing.
- maceurt 8y agoC is different than python though. Python already forces readability things like forcing indent and newline. In most scenarios making a python program in as few lines as possble will make the program more readible.
- michaelcampbell 8y ago> but I cannot train my brain to intuitively view that line as it will be interpreted. I... don't believe you. "Will not", "refuse to"? Sure. But "cannot", no.
- snazz 8y agoThis feels very Perl-y in the example given, in that it requires that you know what yet another operator means to read code that uses it. Since Python is supposed to be “executable pseudocode” (roughly), this kind of new operator might increase the amount of learning that a beginner has to do to read others’ code. I hope that this decision does not pave the way for more like it, because it would make Python code much less readable to someone who hasn’t studied the new operators yet.
- Alex3917 8y agoYeah I feel like this is going to lead to another situation like with Python classes, where it's fine (and good!) to use them in libraries but generally not so good to use them in user code. The next time I update my own style guide it will probably say something like it's ok to use the walrus operator in service methods, but not in views, since views should be readable even to front end devs and PMs who don't know any Python or Django.
- michaelcampbell 8y ago"readability" is such a crap excuse for or against things. Everything is unreadable until you learn to read it. Reading is simply a mapping of what you see, to internalized meaning. Even if you accept that there is some continuum of "...ability" of these things, it's completely arbitrary and unique to everyone as to where the line between "this should be in" and "this should be out" lies.