Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66646 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2014-02-19 08:14 +1100 |
| Last post | 2014-02-19 08:14 +1100 |
| Articles | 1 — 1 participant |
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.
Re: Bad Code Snippet of the Day Chris Angelico <rosuav@gmail.com> - 2014-02-19 08:14 +1100
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-02-19 08:14 +1100 |
| Subject | Re: Bad Code Snippet of the Day |
| Message-ID | <mailman.7125.1392758081.18130.python-list@python.org> |
On Wed, Feb 19, 2014 at 7:41 AM, 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.
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
Back to top | Article view | comp.lang.python
csiph-web