[Python-ideas] syntax to continue into the next subsequent except block

V James Powell py.vjp at vivekpowell.com
Thu Sep 20 17:22:30 CEST 2012


> I think it would be useful if there was a way to skip into the next except
> block, perhaps with continue as I think it's currently always illegal to use
> in an except block. I don't believe there's currently a way to do this.

I would like to share that the original suggestion inspired me on the following
(mis-)adventure.

Cheers,
V James Powell

switch = lambda val: type('', (BaseException,), {'val': val})()
case = lambda pred: type('', (type,), {'__subclasscheck__':
   staticmethod(lambda x: pred(x.val))})('',(BaseException,),{})
default = BaseException

#x = '%s'
x = 12
try:raise    switch(x)
except         case( lambda x: x % 2 == 0 ):
 	             print "It's even!"
except         case( lambda x: x % 2 == 1 ):
 	             print "It's odd!"
except         default:
                  print "It's a joke!"



More information about the Python-ideas mailing list