RFC: Assignment as expression (pre-PEP)

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 5 16:34:45 EDT 2007


Carsten Haese <carsten at uniqsys.com> wrote:

> If that's your only justification for this proposal, that's almost
> certainly not enough to convince anybody of its necessity. Your code
> example should be rewritten as a loop:
> 
> match_actions = [(my_re1, action1),
>                  (my_re2, action2),
>                  ...]
> 
> for my_re, action in match_actions:
>   if my_re.match(exp):
>      action(exp)
>      break
> 

Depending on what his 'do something with temp' actually was, it may or may 
not be easy to rewrite it as a for loop. However, even if a for loop isn't 
an obvious replacement other solutions may be appropriate such as combining 
the regular expressions to a single regex with named groups and/or using 
the command pattern.

If assignment was an expression that only addresses one problem with 
the sample code. It still leaves his code with excessive repetition and 
probably with an excessively long function that calls out to be refactored 
as a group of smaller methods.



More information about the Python-list mailing list