Shortcut needed: avoiding temporary variables with regular expressions

Alex Martelli aleaxit at yahoo.com
Sat Nov 6 17:26:47 EST 2004


Pekka Niiranen <pekka.niiranen at wlanmail.com> wrote:

> Hi there,
> 
> I am using regular expressions like this:
> 
> matcher = re.compile(r'(.*)\s(.*)', re.UNICODE)
> tmp = matcher.search(mystring)
> if tmp:
>       myvariable = tmp.group(1)
> 
> The idea is that group(1) is accessed only if
> it was found from 'mystring'. Is there a way
> to avoid the usage of 'tmp' -variable?

Yeah, quite a few recipes on the cookbook could help, starting with one
of mine, many years old now, about how to do assign-and-test together in
Python (the only use case being to transcribe faithfully an algorithm
coming from languages that focus on assign-and-test, such as Perl or C;
once you move the algorithm fully to Python, it's best to use Python
idioms, of course).

In my old recipe,, you need to have somewhere an auxiliary class and
instance thereof, such as:

class Data(object):
    def set(self, value):
        self.value = value
        return value
d = Data()

then, you can do:

if d.set(matcher.search(mystring)):
    myvariable = d.value.group(1)

This is very general and generic.  For your specific case, you could do:

try: myvariable = matcher.search(mystring).group(1)
except (AttributeError, TypeError): pass

i.e., just try to access .group even if the search results in None, and
simply catch the error that happens when you do (I think it will be an
AttributeError, but I'm not sure that holds in all versions you care
about, so I threw in a TypeError for good measure;-).


Alex



More information about the Python-list mailing list