Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'interpreter': 0.05; 'exception,': 0.09; 'pep': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'elsewhere,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'reedy': 0.16; 'skip:g 50': 0.16; 'stupidity': 0.16; 'discussions': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'value.': 0.19; 'feb': 0.22; 'rules': 0.22; 'cc:addr:python.org': 0.22; 'affects': 0.24; 'subject:Code': 0.24; 'cc:2**0': 0.24; 'code:': 0.26; 'asking': 0.27; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'raise': 0.29; 'message- id:@mail.gmail.com': 0.30; 'obscure': 0.31; 'piece': 0.31; 'prints': 0.31; 'subject:the': 0.34; 'except': 0.35; 'received:google.com': 0.35; 'does': 0.39; 'sure': 0.39; 'how': 0.40; 'is.': 0.60; 'new': 0.61; 'course': 0.61; 'simple': 0.61; "'you": 0.84; 'subject:Day': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=mUYDtyVKvDcEqhwpzXiPXpbVcJLJeoAe9LypZzBdfwA=; b=ozUtHVlTj6UHtR5e+fQpyKklk+2aNbCx5YPDAQhghh8il/RH3wsC+ewNgOoDvs8/xK NwuL0oVNlJPvLUQkuGSmHE4mISq2T3x5jysnVmzuvbjHfAKsSgyj32PLzxPeegDMDLol 1IRgk8fO+kvEty3JbFq/GlQcjBRudPCIiHeSS1QdUqZXKXVdAuWp7wMxm1ze0DyQEVE1 Izgj+XAzWZvgz3Kh9E1qupN4tKpaG1a+Y3ippAtdKqhepFSRefPSqSyx1Sa2z9pRtPYZ 8Bp9RUwCNlzgBhu6ZQaV10nwfJdx9CfwJCnUkZ6fQxZFVg+DXOg2Uv5psxEtEUjILWVR 1dJQ== MIME-Version: 1.0 X-Received: by 10.68.98.3 with SMTP id ee3mr35721068pbb.31.1392758078153; Tue, 18 Feb 2014 13:14:38 -0800 (PST) In-Reply-To: References: Date: Wed, 19 Feb 2014 08:14:37 +1100 Subject: Re: Bad Code Snippet of the Day From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392758081 news.xs4all.nl 2941 [2001:888:2000:d::a6]:44719 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66646 On Wed, Feb 19, 2014 at 7:41 AM, Terry Reedy 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