Newbie: Switch Case

Brian Quinlan brian at sweetapp.com
Mon May 20 13:08:04 EDT 2002


Marcus wrote:
> Is there any switch case control structure in python ? 
> what is the syntax ? 
 
No, there isn't. You can either use an elif block or a dictionary e.g.

if tag == 'p':
	handle_paragraph()
elif tag == 'br':
	handle_break()
elif tag == 'body':
	handle_body()
else:
	handle_default()


or:

case = { 'p'    : handle_paragraph,
         'br'   : handle_break,
         'body' : handle_body }

case.get(tag, handle_default)()

Cheers,
Brian






More information about the Python-list mailing list