Bad Code Snippet of the Day

Roy Smith roy at panix.com
Tue Feb 18 20:13:05 EST 2014


In article <mailman.7122.1392756159.18130.python-list at python.org>,
 Terry Reedy <tjreedy at udel.edu> wrote:

> On 2/18/2014 11:47 AM, Chris Angelico wrote:
> > I call this "Russian Exception Roulette". It came about because of
> > some discussions on python-ideas regarding the new PEP 463 and
> > exception handling.
> >
> > try:
> >      exc = getattr(__builtins__,random.choice(list(filter(lambda x:
> > x.endswith("Error"),dir(__builtins__)))))
> >      f()
> > except exc:
> >      print("You win!")
> >
> > Given a function f(), defined elsewhere, what will this do?
> 
> I am not sure what you are asking or what your point is. If f happens to 
> raise the randomly selected exception, it prints 'You win!', otherwise 
> it does nothing.

def f():
   print("You win!")

problem solved :-)



More information about the Python-list mailing list