How to do this in Python...

Carl Banks imbosol at vt.edu
Sat Jan 25 17:25:25 EST 2003


Michael Tiller wrote:
> My thanks go out to those people who mentioned the "holder" idiom (rather
> than just throwing up their arms in ideological disgust at the idea of
> somebody even suggesting such a construct)

Honestly, what do you expect, considering your original post?  When
you call the following code snippet "overly verbose":

   match = re.match(...)
   if match:
       ...

people are going to get ideological on you; rightly so.  You never
said why you wanted the assignment expression, and it turns out your
intended use was the only quasi-acceptable use for it.

If you had said "Python is incomplete because you can't do this
set-and-test idiom" instead of "Python is incomplete because it
doesn't allow assignment inside if," you would have gotten less
ideology.


> since the "holder" idiom was
> exactly what I needed because it allows me to use if..elif..else without the
> easy to misunderstand syntax of C and C++.

People often say Python is defective or incomplete for not allowing
assignment inside if-conditions, because you can't do this silly
idiom.  Yet it never seems to occur to anyone that there are other
ways to accomplish this besides assignment-if.

I'm happy the holder idiom works for you.  I prefer to do it this way,
although I rarely do it:


    for i in [1]:
        m = re.match(...)
        if m:
            ...
            break

        m = re.match(...)
        if m:
            ...
            break


-- 
CARL BANKS




More information about the Python-list mailing list