How best to write this if-else?
Mike C. Fletcher
mcfletch at home.com
Sat Apr 21 18:08:12 EDT 2001
You can actually just use:
def meaningful_name_1( text ):
...
re_dispatch = [
(re.compile(blah), meaningful_name_1 ),
(re.compile(blah2), meaningful_name_2 ),
...
]
for re, func in re_dispatch:
...
Instead of the dictionary.
Enjoy,
Mike
-----Original Message-----
From: Ben Wolfson [mailto:wolfson at uchicago.edu]
Sent: Saturday, April 21, 2001 17:28
To: python-list at python.org
Subject: Re: How best to write this if-else?
In article <roy-D0E658.17013521042001 at news.panix.com>, "Roy Smith"
<roy at panix.com> wrote:
> What if I want to execute different code depending on which expression I
> matched? Something along the lines of (pseudocode):
...
def meaningful_name_1(matchobject):
do_complicated_processing(matchobject.group(1))
def meaningful_name_2(matchobject): ...etc
re_dispatch = {re.compile(blah):meaningful_name_1,
re.compile(durh):meaningful_name_2
..etc
}
for re, func in re_dispatch.items():
...
More information about the Python-list
mailing list