case/switch statement?

Martin Miller ggrp1.20.martineau at dfgh.net
Wed Jun 22 16:14:19 EDT 2005


Skip Montanaro wrote:
> Terry> Yeah, and I find this even more so:
>
>     Terry> case =  {
>     Terry>         5: do_this,
>     Terry>         6: do_that,
>     Terry>         }
>     Terry> case.get(x, do_default)()
>
>     Terry> Which is looking pretty close to a case statement, anyway.
>
> Sure, modulo namespace issues.
>
> Skip

The namespace issue alluded to doesn't exist when using the very
similar approach suggested earlier in this thread by Andrew Durdin --
shown again below in a [very] contrived example illustrating access to
the local namespace:

	greeting = "Hello there, "
	x = 2

	exec {
	    1: """greeting += 'Tom'""",
	    2: """greeting += 'Dick'""",
	    3: """greeting += 'Harry'""",
	}.get(x, """greeting += 'Unknown'""")
	
	print greeting

Martin




More information about the Python-list mailing list