Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66642 > unrolled thread

Re: Bad Code Snippet of the Day

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-02-18 15:41 -0500
Last post2014-02-18 20:13 -0500
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Bad Code Snippet of the Day Terry Reedy <tjreedy@udel.edu> - 2014-02-18 15:41 -0500
    Re: Bad Code Snippet of the Day Roy Smith <roy@panix.com> - 2014-02-18 20:13 -0500

#66642 — Re: Bad Code Snippet of the Day

FromTerry Reedy <tjreedy@udel.edu>
Date2014-02-18 15:41 -0500
SubjectRe: Bad Code Snippet of the Day
Message-ID<mailman.7122.1392756159.18130.python-list@python.org>
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.

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



-- 
Terry Jan Reedy

[toc] | [next] | [standalone]


#66660

FromRoy Smith <roy@panix.com>
Date2014-02-18 20:13 -0500
Message-ID<roy-6708E7.20130518022014@news.panix.com>
In reply to#66642
In article <mailman.7122.1392756159.18130.python-list@python.org>,
 Terry Reedy <tjreedy@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 :-)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web