How to do this in Python...

Michael Tiller mtiller at
Thu Jan 23 22:40:02 CET 2003

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


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?


More information about the Python-list mailing list