Return value of an assignment statement?

Jeff Schwab jeff at schwabcenter.com
Sat Feb 23 12:20:21 EST 2008


Arnaud Delobelle wrote:
> On Feb 23, 3:44 pm, Jeff Schwab <j... at schwabcenter.com> 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. :)



More information about the Python-list mailing list