[Python-Dev] Re: Re: switch statement

Fredrik Lundh fredrik at pythonware.com
Thu Apr 21 17:22:29 CEST 2005


Michael Chermside wrote:

> There is one exception... matching strings. There we have a powerful
> means of specifying patterns (regular expressions), and a multi-way
> branch based on the content of a string is a common situation. A new
> way to write this:
> 
>    s = get_some_string_value()
>    if s == '':
>        continue;
>    elif re.match('#.*$', s):
>        handle_comment()
>    elif s == 'DEFINE':
>        handle_define()
>    elif s == 'UNDEF':
>        handle_undefine()
>    elif re.match('[A-Za-z][A-Za-z0-9]*$', s):
>        handle_identifier()
>    else:
>        syntax_error()
> 
> would be might be nice, but I can't figure out how to make it work
> more efficiently than the simple if-elif-else structure, nor an
> elegent syntax.

somewhat related:

http://mail.python.org/pipermail/python-dev/2003-April/035075.html

</F>



More information about the Python-Dev mailing list