How to do this in Python...

Nicola Paolucci durdn at yahoo.it.oops!.invalid
Thu Jan 23 17:50:18 EST 2003


Hi Michael,
the Python Cookbook is your friend. Recipe 1.9 is about Assigning and 
Testing with One Statement.

Look at page 18: http://www.oreilly.com/catalog/pythoncook/chapter/ch01.pdf

Best regards,
     Nicola Paolucci

Michael Tiller wrote:
> I'm puzzled by what seems like a missing piece of functionality in Python.
> I suspect there is a simple way to do what I want to do and I'm guessing
> someone here can point it out to me.  Here is the issue:
> 
> In C++, I could write a statement like this:
> 
> if (match=re.match(pattern, string))
>   // do something with the match object
> 
> But I cannot figure out how to do the equivalent thing in Python.  The idea
> here is to have an assignment statement within an if statement so that I
> don't have to call match twice or obfuscate the code.  It seems to me that
> my current options are:
> 
> match = re.match(pattern, string)
> if match:
>   // do something with match object
> 
> -or-
> 
> if re.match(pattern, string):
>     match = re.match(pattern, string)
> 
> The first one seems unnecessarily verbose and the second one is inefficient.
> Are there any other options?
> 
> --
> Mike
> 
> 
> 

-- 
#Remove .oops!.invalid to email or feed to Python:
'Tmljb2xhIFBhb2x1Y2NpIDxuaWNrQG5vdGp1c3RjYy5jb20+'.decode('base64')





More information about the Python-list mailing list