How to do this in Python...

Chad Netzer cnetzer at mail.arc.nasa.gov
Thu Jan 23 18:43:43 EST 2003


On Thursday 23 January 2003 13:40, Michael Tiller wrote:
>  It seems to me that my current options are:
>
> match = re.match(pattern, string)
> if match:
>   // do something with match object

This is the 'pythonic' way, since it separates the test from the 
assignment.  To be pedantic, it should probably be:

match = re.match(pattern, string)
if match is not None:
    # do something with match object

Since if's can test for more than boolean values, and match is defined 
to return None when it fails to match anything, not a False value.  
It's an adjustment coming from C/C++, but a small one.

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer






More information about the Python-list mailing list