Arnaud Delobelle wrote:
> On Feb 23, 3:44 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote:
>>      actions = (
>>              ('some_string', do_something),
>>              ('other_string', do_other_thing))
>>
>>      def find_action(pattern):
>>          for string, action in actions:
>>              m = pattern.match(string)
>>              if m:
>>                  return action
>>          return do_default_thing
>>
>>      find_action(re.compile('some pattern'))()
> 
> You don't need to pass the pattern, just pass the match function:
> 
> def find_action(match, actions=actions, default_action=None):
>     for string, action in actions:
>         if match(string):
>             return action
>     return default_action
> 
> find_action(re.compile('some pattern').match)()

That's cool. :)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to