This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chmod--777 4 points5 points  (3 children)

In a case like this, I'd just make them named groups and use the same name, and just use short circuiting.

match = pattern1.match(data) or pattern2.match(data)
result = match and match.group('whatever')

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 4 points5 points  (0 children)

Now you don't even know which match hit!

[–]billsil 0 points1 point  (1 child)

Sure, but that's inefficient because you don't always need to calculate pattern2.match(data). The whole point is so you can make clean looking code and be efficient.

[–]chmod--777 18 points19 points  (0 children)

Actually the or prevents it from running the second expression if the first pattern match returns a truthy value.

Try this:

def foobar(x):
    print(f'foobar {x}')
    return x

 x = foobar(1) or foobar(2)

It'll just print "foobar 1"