Reply to post: Re: Is this equivalent to the following, widely recognised as horrible in any C-like?

Python creator Guido van Rossum sys.exit()s as language overlord

FeRDNYC

Re: Is this equivalent to the following, widely recognised as horrible in any C-like?

And, in fact, PEP 572 isn't JUST about list comprehensions. It adds in-expression assignment generally, a very common language pattern which Python previously lacked. So, while these two expressions are equivalent:

foreach my $x (@input_data) { # Perl

for x in input_data: # Python

other common in-loop assignments such as (again, Perl):

while (my $x = $parser->get_token()) {

do_stuff_to_x

}

had no direct syntactic Python equivalent. But with PEP 572, this can now be written in Python as:

while x := parser.get_token():

do_indented_stuff_to_x

In-expression assignments like that are generally a common pattern. They can be abused, of course. But C is the king of "enough rope to hang yourself" so a feature being open to abuse isn't an argument against the feature. It's against abusing it.

(And I would argue that the list comprehension form does not count as abuse. There's a reason they went with := instead of =, and it was to avoid exactly the type of horrible C code you mentioned.)

Because while

if (a = f(b)) {

is absolutely horrible C due to its "hidden" assignment, if the syntax were

if (a := f(b)) {

that would be far less problematic, because it's clearly different from a == test for equality.

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Biting the hand that feeds IT © 1998–2019