Bad Code Snippet of the Day

Chris Angelico rosuav at gmail.com
Tue Feb 18 17:47:15 CET 2014


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?

Note that this was developed on Python 3.4, and some of the details
may be different on other versions (especially 2.x).

ChrisA



More information about the Python-list mailing list