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?

Soooooorta, but you're also inside a for loop (which is what a list comprehension is, an expression evaluated for every member of a given list), so there's also a sense in which it's a shorthand for (in Perl):

foreach my $x ( @input_data ) {

my $y = f($x);

push(@output_data, [$x, $y, $x/$y]) if ($y > 0);

}

Yes, the assignment is inside the if statement, but that's because you've already compressed the for loop into a one-line comprehension and there's really no way to locate it anywhere else. The point is to call f(x) only ONCE, assign it to y, and then output the tuple iff y > 0;

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