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


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

Bad Code Snippet of the Day

Started byChris Angelico <rosuav@gmail.com>
First post2014-02-19 03:47 +1100
Last post2014-02-19 08:21 +1100
Articles 3 — 2 participants

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


Contents

  Bad Code Snippet of the Day Chris Angelico <rosuav@gmail.com> - 2014-02-19 03:47 +1100
    Re: Bad Code Snippet of the Day Rick Johnson <rantingrickjohnson@gmail.com> - 2014-02-18 13:10 -0800
      Re: Bad Code Snippet of the Day Chris Angelico <rosuav@gmail.com> - 2014-02-19 08:21 +1100

#66637 — Bad Code Snippet of the Day

FromChris Angelico <rosuav@gmail.com>
Date2014-02-19 03:47 +1100
SubjectBad Code Snippet of the Day
Message-ID<mailman.7119.1392742039.18130.python-list@python.org>
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

[toc] | [next] | [standalone]


#66645

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2014-02-18 13:10 -0800
Message-ID<5f3e508e-d4d6-4573-b562-e393f7183f5a@googlegroups.com>
In reply to#66637
On Tuesday, February 18, 2014 10:47:15 AM UTC-6, 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?

For get about "f()", it will throw a NameError since "random" 
was not imported. 

Beyond that this code (either conscientiously or unconsciously)
exposes the onerous and obfuscation of a language design
that coddles a global function nightmare paradigm over the
elegance of true OOP -- talk about cutting off your nose
just to spite your face!

[toc] | [prev] | [next] | [standalone]


#66648

FromChris Angelico <rosuav@gmail.com>
Date2014-02-19 08:21 +1100
Message-ID<mailman.7127.1392758489.18130.python-list@python.org>
In reply to#66645
On Wed, Feb 19, 2014 at 8:10 AM, Rick Johnson
<rantingrickjohnson@gmail.com> wrote:
> Beyond that this code (either conscientiously or unconsciously)
> exposes the onerous and obfuscation of a language design
> that coddles a global function nightmare paradigm over the
> elegance of true OOP -- talk about cutting off your nose
> just to spite your face!

Humour me, Rick. Pretend I've been awake for 22 hours straight, am
wired on sleep-deprivation and energy drinks, and have no idea what
you're talking about. How exactly does the ability to dynamically
determine which exception to catch coddle a functional paradigm, and
how is it not object oriented?

Also, my face doesn't much care for my nose. It gets in the way. If I
cut my nose off, my face would be happy with me.

ChrisA

[toc] | [prev] | [standalone]


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


csiph-web