LWN: Comments on "PEP 572 and decision-making in Python" https://lwn.net/Articles/757713/ This is a special feed containing comments posted to the individual LWN article titled "PEP 572 and decision-making in Python". en-us Mon, 15 Sep 2025 15:38:29 +0000 Mon, 15 Sep 2025 15:38:29 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Yet more evidence for Wadler's law https://lwn.net/Articles/760356/ https://lwn.net/Articles/760356/ mina86 <div class="FormattedComment"> Weird, my Polish layout has ‘ü’ there. ;)<br> </div> Thu, 19 Jul 2018 16:52:23 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/759806/ https://lwn.net/Articles/759806/ gdiscry <p>That's the issue with toy examples like mine and the ones in the article. If it's a common pattern there's a pretty good chance that you can do it in a simple, optimized and readable way. And then the reader misses the point because the syntax specific to the common case doesn't apply to more complex code.</p> <p>A better example for iter would be to read a binary file by chunk using <code>for chunk in iter(lambda: f.read(CHUNK_SIZE), b''):</code>. To my knowledge, there isn't a ready made function or method that does the same, contrary to file.readline and file.__iter__.</p> <p>We can also see that the code is slightly less readable than the readline example. Furthermore, the complexity increases even more once the stop condition cannot be expressed as a simple equality to a sentinel value.</p> <p>I have to admit that I find <code>while chunk := f.read(CHUNK_SIZE):</code> more readable than the other ways to do it and it looks like the readability remains good even in more complex cases.</p> Fri, 13 Jul 2018 22:14:11 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/759794/ https://lwn.net/Articles/759794/ gdamjan the default iterator of python files actually generates lines <pre> &gt;&gt;&gt; fp = open('/etc/os-release') &gt;&gt;&gt; next(fp) 'NAME="Arch Linux"\n' </pre> Fri, 13 Jul 2018 19:25:00 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/759793/ https://lwn.net/Articles/759793/ ErikF "&lt;-" has the same problem as the <a rel="nofollow" href="https://stackoverflow.com/questions/7573978/what-does-mean-in-c">compound assignment operators in early versions of C</a> (i.e. "=+", "=-", etc.): it's ambiguous in high-traffic places. How would you then do a comparison like "a&lt;-b"? Fri, 13 Jul 2018 19:07:30 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/759713/ https://lwn.net/Articles/759713/ nix <div class="FormattedComment"> (Note for clarity: I'm not talking about Thompson's B here, but Abrial's B: &lt;<a href="https://en.wikipedia.org/wiki/B-Method">https://en.wikipedia.org/wiki/B-Method</a>&gt;. Whether it was only ever visibly used on French safety-critical systems because it was a formal method, because it was ring-fenced, and had only one implementation, or because like Z it used insane numbers of unprounceable symbols that made it really hard to actually talk about the programs you were writing, I'm not sure. Heck it might just be because it was French and your average Silicon Valley VC doesn't speak much French...)<br> </div> Fri, 13 Jul 2018 10:47:55 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/759703/ https://lwn.net/Articles/759703/ zdzichu <div class="FormattedComment"> It's RightAlt+y on my keyboard layout in Linux (Polish). <br> </div> Fri, 13 Jul 2018 06:12:09 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/759696/ https://lwn.net/Articles/759696/ OrbatuThyanD <div class="FormattedComment"> having spent 18 months pushing through an incredibly inconsequential pep that got bike-shedded to death (eventually accepted, you python 3.2+ folks are potentially benefiting), i'm oddly not surprised that guido is quitting as bdfl after this. dealing with the python devs on that pep made me never want to write python again.<br> </div> Fri, 13 Jul 2018 02:07:21 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/759690/ https://lwn.net/Articles/759690/ zblaxell <div class="FormattedComment"> <font class="QuotedText">&gt; Though editors could translate &lt;- into ←, one wonders why you didn't just use &lt;- in the first place and avoid this probably-nearly-universal difficulty.</font><br> <p> Indeed, "←" is M-bM-^FM-^P, which is even worse than "&lt;-" at 300 baud. Adoption would be slow for the first 15 years while the C development community waited for Unicode to be invented.<br> </div> Fri, 13 Jul 2018 00:36:42 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/759303/ https://lwn.net/Articles/759303/ nix <div class="FormattedComment"> <font class="QuotedText">&gt; Someone who came up with a new programming language today would probably not think twice about using “←” for assignment</font><br> <p> The lesson of B and Z: if your wonderful new language uses lots of symbols that aren't on people's keyboards, uptake will be slow. (Though editors could translate &lt;- into ←, one wonders why you didn't just use &lt;- in the first place and avoid this probably-nearly-universal difficulty.)<br> <p> (Mind you, a bigger lesson of B: if you ring-fence a language around with copyrights and everything else you can think of, and allow only one implementation on one platform with no FFI facilities, you can expect not too terribly many people to take it up...)<br> <p> </div> Sun, 08 Jul 2018 15:12:17 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758739/ https://lwn.net/Articles/758739/ anselm <p> The convention of <tt>=</tt> as the assignment operator and <tt>==</tt> as the equality operator actually arose when Ken Thompson designed the B programming language, the predecessor of C. B in turn is derived from Martin Richards' programming language, BCPL, which does use <tt>:=</tt> for assignment, but one of Thompson's goals when designing B was apparently “reducing the number of non-whitespace characters in a typical program” [<a href="https://en.wikipedia.org/wiki/B_(programming_language)">Wikipedia article on B</a>]. </p> Sat, 30 Jun 2018 12:41:52 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758727/ https://lwn.net/Articles/758727/ HelloWorld <div class="FormattedComment"> I don't buy that at all. A single additional character per assignment is very unlikely to be a problem, even on a 300-baud tty.<br> </div> Sat, 30 Jun 2018 07:36:14 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758713/ https://lwn.net/Articles/758713/ HelloWorld <div class="FormattedComment"> <font class="QuotedText">&gt; That's funny: C's assignment operator being an expression instead of a function is viewed as a source of errors: if (toto = FOO) instead of if (toto == FOO)</font><br> The problem here isn't that the assignment is an expression but that is has the type of the left operand. If assignment expressions had type void, you couldn't make that mistake.<br> </div> Fri, 29 Jun 2018 22:59:24 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758680/ https://lwn.net/Articles/758680/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; You're splitting hair, the different between a statement and a function which will return always void..</font><br> <p> The difference is significant in terms of language design. Unification of statements and expressions does not imply that all expressions must produce a value, any more than a function call—which is always considered an expression in C—must produce a value. The point is merely to avoid having two distinct kinds of syntax. Instead of "statements" and "expressions", where statements can include expressions but not vice-versa, you'd simply have "expressions which return a usable value" and "expressions which return void". A function body would just be a single expression to be evaluated.<br> <p> Add to that treating 'void' as a standard type with exactly one possible value (and thus no runtime representation), suitable for declaring variables, fields, parameters, etc., and some sort of local variable-binding expression similar to LISP's 'let' form, and you enable much more straightforward and expressive generic programming which currently can only be achieved through nonstandard extensions such as GCC's statement-expressions and __builtin_choose_expr() / __builtin_types_compatible_p().<br> </div> Fri, 29 Jun 2018 17:27:29 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758670/ https://lwn.net/Articles/758670/ anselm <blockquote><em>Simply changing the names to '=' for equality and something like ':=' for assignment would have eliminated the issue without any change in semantics.</em></blockquote> <p> That makes a lot of sense, especially with 20/20 hindsight. It's, however, just as well to remember that when C was invented, the method of choice to talk to your Unix machine (a PDP-11) would have been a 300-baud teletype printer. The original Unix developers were pragmatic people and they wanted to avoid superfluous keystrokes, hence <tt>ls</tt> and <tt>rm</tt> instead of <tt>list</tt> and <tt>remove</tt>, and, because C programs tend to contain way more assignments than equality comparisons, <tt>=</tt> for assignment and <tt>==</tt> for equality. (OTOH, Niklaus Wirth, who invented Pascal at about the same time, was not a pragmatist at all, so Pascal does have <tt>:=</tt> for assignment and <tt>=</tt> for equality.) </p> <p> Someone who came up with a new programming language today would probably not think twice about using “←” for assignment, which should eliminate the nasty ambiguity. But the heritage of the 300-baud teletype is still with us today and unlikely to go away anytime soon. </p> Fri, 29 Jun 2018 15:08:29 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758669/ https://lwn.net/Articles/758669/ renox <div class="FormattedComment"> <font class="QuotedText">&gt; To prevent assignment from being used in a context where a value is expected the result type of an assignment could simply be defined as void,</font><br> <p> You're splitting hair, the different between a statement and a function which will return always void..<br> <p> <p> <p> </div> Fri, 29 Jun 2018 15:00:42 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758660/ https://lwn.net/Articles/758660/ nybble41 <div class="FormattedComment"> <font class="QuotedText">&gt; C's assignment operator being an expression instead of a function is viewed as a source of errors</font><br> <p> The errors are not due to assignment being an expression rather than a statement. To prevent assignment from being used in a context where a value is expected the result type of an assignment could simply be defined as void, while still treating it as an expression. The real issue, though, is the similarity between '=' and '==' and the fact that '=' is commonly used in math and elsewhere to represent equality, not assignment. Simply changing the names to '=' for equality and something like ':=' for assignment would have eliminated the issue without any change in semantics.<br> </div> Fri, 29 Jun 2018 14:38:11 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758634/ https://lwn.net/Articles/758634/ excors <div class="FormattedComment"> Isn't that largely solved by modern compilers producing warnings in that case? (or in Java by making it a compile error, except in the unlikely case that the variable is a boolean). Personally I haven't seen it be a significantly common source of bugs, compared to the dozens of other ways that bugs can be introduced by small syntax errors, so it doesn't seem worth hobbling the whole language just for that.<br> </div> Fri, 29 Jun 2018 13:25:37 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758632/ https://lwn.net/Articles/758632/ adobriyan <div class="FormattedComment"> It is hard to typo SETF into EQ or =. But it is very easy to typo '==' into "=".<br> <p> Python has different operators for bitwise and logical operations which C should copy (already copied with iso646.h except if you try to use them, men will stop shaking hands with you).<br> </div> Fri, 29 Jun 2018 13:17:39 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758630/ https://lwn.net/Articles/758630/ renox <div class="FormattedComment"> <font class="QuotedText">&gt; Of course the real problem here is not the discussion but Python's broken language design. John McCarthy figured out in 1958 that the artifical separation of statements and expressions is completely unnecessary, and yet Van Rossum felt it necessary to make that mistake again 40 years later.</font><br> <p> That's funny: C's assignment operator being an expression instead of a function is viewed as a source of errors: if (toto = FOO) instead of if (toto == FOO),<br> which creates ugly things like Yoda speak if (FOO == toto) or the non-standardized (but less ugly) "double parenthesis" pattern.<br> <p> <p> <p> <p> </div> Fri, 29 Jun 2018 12:53:40 +0000 Yet more evidence for Wadler's law https://lwn.net/Articles/758596/ https://lwn.net/Articles/758596/ HelloWorld <div class="FormattedComment"> “<br> In any language design, the total time spent discussing<br> a feature in this list is proportional to two raised to<br> the power of its position.<br> 0. Semantics<br> 1. Syntax<br> 2. Lexical syntax<br> 3. Lexical syntax of comments<br> ”<br> <p> Of course the real problem here is not the discussion but Python's broken language design. John McCarthy figured out in 1958 that the artifical separation of statements and expressions is completely unnecessary, and yet Van Rossum felt it necessary to make that mistake again 40 years later.<br> </div> Thu, 28 Jun 2018 21:00:56 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/758126/ https://lwn.net/Articles/758126/ ewen <div class="FormattedComment"> I do miss "assign and test for truthiness" at times in Python (especially re.match, and loops over functions returning content until EOF, etc), but also dislike the bugs this can introduce in C code. Reusing the exception handling / context manager syntax ("... FUNCTION_OUTPUT as VAR") seems like an ideal solution to both problems to me, as it's impossible to confuse with "oh, I meant to compare values", it's consistent with other Python syntax which helps readability, and it is still almost as concise as the other variations being suggested (:=, etc). <br> <p> In addition the "... FUNCTION_OUTPUT as VAR" would also trivially allow treating that specific VAR as block scoped (eg, as it is implicitly is a context manager / exception handler) without surprising anyone that happened. As with other Python scoping, if the variable already existed earlier, it could update the outer scoped variable instead of creating a new block scoped one.<br> <p> Good suggestion!<br> <p> Ewen<br> </div> Sun, 24 Jun 2018 23:53:24 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/758038/ https://lwn.net/Articles/758038/ epa <blockquote>All variables in python are references, so assigning 5 to x never changes a or b ever.</blockquote> That's the doctrine -- but as I noted, the language is structured so that the question can't even arise. Perhaps a better example would be <pre> a = 5 def foo(): a = 6 f = lambda x: a := 7 return f print(a) f = foo() print(a) f(0) print(a) </pre> Inside the function <code>foo()</code> the first assignment <code>a = 6</code> only affects a local variable <code>a</code>, not the outer one. But which <code>a</code> should be affected when you call <code>f(0)</code>? The one in the caller's scope or the one in scope where the lambda expression was declared? Fri, 22 Jun 2018 21:34:21 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/758027/ https://lwn.net/Articles/758027/ k8to <div class="FormattedComment"> In this case, I use the while True, but i've used the generator helper in scenarios where the while True was awkward. I've gotten challenged on it as being too exotic/weird though. Maybe they were right?<br> </div> Fri, 22 Jun 2018 16:02:09 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/758026/ https://lwn.net/Articles/758026/ k8to <div class="FormattedComment"> I don't see how this scenario is a question. All variables in python are references, so assigning 5 to x never changes a or b ever.<br> </div> Fri, 22 Jun 2018 15:59:29 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757977/ https://lwn.net/Articles/757977/ gdiscry <p>You don't even have to implement a new function, it <a href="https://docs.python.org/3/library/functions.html#iter">already exists</a>:</p> <pre> with open(…) as f: for line in iter(f.readline, ''): … </pre> Thu, 21 Jun 2018 16:45:39 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757973/ https://lwn.net/Articles/757973/ smurf <p>You can rewrite the readline example to use a for loop, with a small generator helper: </p> <pre> def as_loop(fn, *args): while True: res = fn(*args) if not res: return yield res … f = open(…) for line in as_loop(f.readline): … </pre> <p> assuming that you're opposed to the trivial rewrite of</p> <pre> while True: line = f.readline() if not line: break … </pre> <p> which I personally prefer. </p> Thu, 21 Jun 2018 14:30:51 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757951/ https://lwn.net/Articles/757951/ epa I think it's more than that: the division between expressions and statements runs deep in Python, and makes it different from languages like C, where there is not such a great difference between the two. For example take a list comprehension <pre> a = 1 b = 2 [x+1 for x in a, b] </pre> That works because <code>x+1</code> is an expression. On the other hand, <code>x=1</code> is a statement, so cannot appear there. That means you don't have to consider the semantics of how to assign to each list element, and whether <code>a, b</code> are lvalues or rvalues. Now, if an expression can also perform assignment (as in C), you could have <pre> [x := 5 for x in a, b] </pre> Should this modify the values of <code>a</code> and <code>b</code> in the local scope? Should it make temporary copies of these values, assign 5 to them, and then return them without modifying the originals? Or should it fail at run time with an error that <code>a</code> is not modifiable? Thu, 21 Jun 2018 07:41:34 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757947/ https://lwn.net/Articles/757947/ k8to <div class="FormattedComment"> If we must have inline assignments, this is at least consistent.<br> </div> Thu, 21 Jun 2018 06:33:47 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757944/ https://lwn.net/Articles/757944/ marcH <div class="FormattedComment"> <font class="QuotedText">&gt; But Barry Warsaw believes that PEP 572 is an outlier. Since it changes the syntax of the language, people tend to focus on that without understanding the deeper semantic issues.</font><br> <p> Would it have helped to decouple the two (or three, four...) questions and proceed in incremental stages? I mean discussing and voting incomplete-by-design PEPs like: 1. semantics; 2. syntax; 3. lex. Earlier PEPs could be approved even with abstract/incomplete content like for instance this:<br> <p> if ( x :TBD= 5)<br> <p> </div> Thu, 21 Jun 2018 05:10:25 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757941/ https://lwn.net/Articles/757941/ em-bee <div class="FormattedComment"> (with added exception handling)<br> </div> Thu, 21 Jun 2018 02:33:16 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757931/ https://lwn.net/Articles/757931/ em-bee or like this? <pre> for i in [(p1,1),(p2,2),(p3,3)]: return re.match(i[0], line).group(i[1]) </pre> greetings, eMBee. Thu, 21 Jun 2018 02:23:52 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757937/ https://lwn.net/Articles/757937/ IkeTo I wonder why it cannot be made to be consistent with exception handling syntax and context manager syntax by using the "as" keyword. Like: <pre> if re.match(p1, line) as m: return m.group(1) elif re.match(p2, line) as m: return m.group(2) elif re.match(p3, line) as m: ... </pre> or <pre> while f.readline() as line: ... # process line </pre> Thu, 21 Jun 2018 01:01:24 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757934/ https://lwn.net/Articles/757934/ dw <div class="FormattedComment"> Not just added complication, but added incompatibility. The language has barely shifted in productivity since 2.3-2.5 era (of course this could be debated), yet maintaining compatibility with even 5 year old installs is nigh impossible given the continuous "feature" additions<br> </div> Thu, 21 Jun 2018 00:54:50 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757919/ https://lwn.net/Articles/757919/ tbodt The first example given could also be written like this: <pre> m = re.match(p1, line) if m: return m.group(1) m = re.match(p2, line) if m: return m.group(2) m = re.match(p3, line) if m: return m.group(3) ... </pre> Wed, 20 Jun 2018 21:01:53 +0000 PEP 572 and decision-making in Python https://lwn.net/Articles/757893/ https://lwn.net/Articles/757893/ k8to <div class="FormattedComment"> It's another in a long line of python mistakes, making the language more complicated for very minor benefit.<br> <p> If you want to handle pattern match better, then add a pattern match construction. That would have a lot of power, clarity and efficiency of expression. Inline assignments let you make a lot of things less explicit with very small gains in efficiency of representation, no matter how you spell them.<br> </div> Wed, 20 Jun 2018 18:59:44 +0000