Can global variable be passed into Python function?

Roy Smith roy at panix.com
Fri Feb 28 09:43:30 EST 2014


In article <87d2i7wbxs.fsf at elektro.pacujo.net>,
 Marko Rauhamaa <marko at pacujo.net> wrote:

> Neil Cerutti <neilc at norwich.edu>:
> 
> > Check out Go's switch statement for an example of what it might
> > look like in Python. Except you'd get it without labeled break or
> > the fallthrough statement.

Python already has a switch statement.  It's just spelled funny...

class Switch(Exception): pass
class Case1(Switch): pass
class Case2(Switch): pass
class Case3(Switch): pass

try:
   raise value
except Case1:
   print "did case 1"
except (Case2, Case3):
   print "did either case 2 or 3"
else:
   print "did default"

No fall-through, however.  I'm sure with a little meta-class magic, you 
could write a Case() which eliminates the need for manually declaring 
the cases.



More information about the Python-list mailing list