RFC: Assignment as expression (pre-PEP)

Carsten Haese carsten at uniqsys.com
Thu Apr 5 16:20:12 EDT 2007


On Thu, 2007-04-05 at 12:51 -0700, TimeHorse at gmail.com wrote:
> I would like to gauge interest in the following proposal:
> 
> Problem:
> 
> Assignment statements cannot be used as expressions.
>
> Performing a list of mutually exclusive checks that require data
> processing can cause excessive tabification.  For example, consider
> the following python snipet...
> 
> temp = my_re1.match(exp)
> if temp:
>   # do something with temp
> else:
>   temp = my_re2.match(exp)
>   if temp:
>     # do something with temp
>   else:
>     temp = my_re3.match(exp)
>     if temp:
>       # do something with temp
>     else:
>       temp = my_re4.match(exp)
> 
> # etc.
> 
> Even with 2-space tabification, after about 20 matches, the
> indentation will take up half an 80-column terminal screen.

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

Hope this helps,

Carsten





More information about the Python-list mailing list