Code style query: multiple assignments in if/elif tree

Rustom Mody rustompmody at gmail.com
Tue Apr 1 01:45:32 EDT 2014


On Tue, Apr 1, 2014 at 3:26 PM, Steven D'Aprano  wrote:

> Haskell has nifty pattern-matching syntax for this that looks quite close
> to the mathematical hybrid function syntax, but in Python, we're limited
> to explicitly using an if. If I were coding this, and I'm not, I'd wrap
> it in a function. One advantage of a state variable rather than a
> continuous time function is that we can do this:
> def accel(state):
>     return {NO_BRAKING: 0.0,
>             LOW_BRAKING: 0.2,
>             MID_BRAKING: 0.425,
>             HIGH_BRAKING: 0.85}[state]

Neat
I would put the dict in a variable. And those _BRAKINGs are GALLing me!

breaking = {NO:0.0, LOW:0.2, MID:0.425:, HIGH:0.85}
def accel(state): return breaking[state]

<Irony>
In using Haskell, I often wish for dicts especially python's nifty
dict-literals
</Irony>




More information about the Python-list mailing list