you could try bound methods The following is probably not the nicest of ways but it shows you how to keep the abstraction in the loop. import re def printRes1(m): print m.group(1) def printRes2(m): print m.group(1) print m.group(2) re1 = (re.compile("demo.*"),printRes1) re2 = (re.compile("testing(.*)for(.*)"),printRes2) all = [re1,re2] str = "testing things for now" for i in all: ma = i[0].match(str) if ma: i[1](ma) Mick -----Original Message----- From: python-list-admin at python.org [mailto:python-list-admin at python.org]On Behalf Of Roy Smith Sent: Sunday, April 22, 2001 9:02 AM To: python-list at python.org Subject: Re: How best to write this if-else? "Ingo Wilken" <Ingo.Wilken at Informatik.Uni-Oldenburg.DE> wrote: > for e in [e1, e2, e3]: > m = e.match(line) > if m: > text = m.group(1) > break > else: > no match found OK, that's pretty neat, but I realize now that my example was unintentionally misleading. The problem is not quite as regular as I made it out to be. What if I want to execute different code depending on which expression I matched? Something along the lines of (pseudocode): if (m = e1.match(line)): text1 = m.group(1) do_complicated_processing (text1) elif (m = e2.match(line)): text1 = m.group(1) text2 = m.group(2) print text1, text2 elif (m = e3.match(line)): return -- http://mail.python.org/mailman/listinfo/python-list
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4