Bad Code Snippet of the Day

2014-02-18 Thread Chris Angelico
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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bad Code Snippet of the Day

2014-02-18 Thread Terry Reedy

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

--
https://mail.python.org/mailman/listinfo/python-list


Re: Bad Code Snippet of the Day

2014-02-18 Thread Chris Angelico
On Wed, Feb 19, 2014 at 7:41 AM, Terry Reedy tjre...@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.

My point is that this is a piece of obscure code: the try block
affects the interpretation of the except that's associated with it.
Just playing around with obfuscated Python, for amusement value.

The rules of Python are so simple that they allow stupidity like this,
and the interpreter just goes, So? Of course that's how it is!. I
like it. :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bad Code Snippet of the Day

2014-02-18 Thread Rick Johnson
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!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bad Code Snippet of the Day

2014-02-18 Thread Chris Angelico
On Wed, Feb 19, 2014 at 8:10 AM, Rick Johnson
rantingrickjohn...@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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bad Code Snippet of the Day

2014-02-18 Thread Roy Smith
In article mailman.7122.1392756159.18130.python-l...@python.org,
 Terry Reedy tjre...@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 :-)
-- 
https://mail.python.org/mailman/listinfo/python-list