James Luberda

Logo

Personal GitHub Page

View My GitHub Profile

5 March 2019

Python: PEP 572

by James Luberda

So there’s really not much I’m contributing here, just taking a moment to reflect on the controversy, and the contribution, of PEP 572. It brought to mind my appreciation of Ruby (an appreciation I fought every step of the way, admittedly, until more or less overnight it became a favorite) as well as Perl, which is also, by definition, un-Pythonic.

In short, Python is adding an operator, := to allow what you can already do in Ruby and Perl with the plain old = operator, which is assign a value inline, i.e. in Perl:

#!/usr/bin/perl
use 5.016;

my $myval;
if (($myval = (1 - 1)))  {
    say $myval;
}
say $myval;

if (($myval = (1 - 0)))  {
    say $myval;
}
say $myval;

Results in:

0
1
1

In the first case, $myval is assigned the value zero (false), so does not meet the if condition, and is only printed once. In the second case, $myval evaluates to 1, so it meets the if condition (true) and is printed twice.

Not terribly exciting, but seeing how something I took for granted in two of my preferred programming languages is the source of conflict (and is a new feature) for another programming language community made me appreciate those languages that much more.