Code style query: multiple assignments in if/elif tree

Chris Angelico rosuav at gmail.com
Mon Mar 31 18:03:54 CEST 2014


On Tue, Apr 1, 2014 at 2:40 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> As a simple layout question, I'd do it like this:
>
> ========================================================================
> if mode == "Brake2":
>     # Already got the brakes fully on
>     distance_to_full_braking_power = 0.0
>     speed_full_brake = curspeed
> elif mode == "Brake1":
>     # The brakes went on one second ago, they're nearly full
>     distance_to_full_braking_power = curspeed - 0.2125
>     speed_full_brake = curspeed - 0.425
> else:
>     # Brakes aren't on.
>     distance_to_full_braking_power = (curspeed - 0.1) + (curspeed - 0.4125)
>     speed_full_brake = curspeed - 0.625
>
> # If we hit the brakes now (or already have hit them), we'll go another
> # d meters and be going at s m/s before reaching full braking power.
> ========================================================================

No particular advantage over the current version - it doesn't simplify
it any, all you've done is break it across more lines.

(The unpacking may not be ideal; as I said, this was a vehicle for
teaching oddments of Python, so I used multiple assignment partly for
the sake of using it.)

Incidentally, if you want to see the code in context, it's here:

https://github.com/Rosuav/runningtime/blob/master/runningtime.py

ChrisA



More information about the Python-list mailing list