Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'syntax': 0.04; 'true,': 0.05; 'subject:code': 0.07; 'python': 0.11; 'def': 0.12; '10:59': 0.16; 'abusing': 0.16; 'false:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda:': 0.16; 'rationale': 0.16; 'redundant': 0.16; 'subject:simple': 0.16; 'true:': 0.16; 'wrote:': 0.18; "python's": 0.19; 'bonus': 0.22; 'mon,': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'on,': 0.29; 'points': 0.29; 'message-id:@mail.gmail.com': 0.30; 'something': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'yield': 0.36; 'so,': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'simple,': 0.60; 'name:': 0.61; 'name': 0.63; 'jul': 0.74; 'good!': 0.84; '2013': 0.98 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:to :content-type; bh=KIw4cOlm8tU7YFgEA/abvcfsz2Y1qexPSOKzWQb6UFs=; b=oDtO6QvnWdTwef//3XBptauXCfyiMnuZSQDmSgRTNu7gGHPUunzB4F7ZvXvHMmv+sC tHt8+PVuYvIkOFFXRnTYBTxJYELETup7IMdEZRlBGTD3V4PcNdzZuRAhAgx97DNTbYho JaUKG3L2k8/7e+AO4xpkeTpsMkLfzTmbZO5tUct0h3C5WKTM+l5A93lAhgnwBIngpGN5 1vyBwqDR7mOjZpyCvMCebmfeGezWnSvuiGlTa9EuUgj1TGrFKddT31FZDITtZQlnEhoM pBRDk4/ptfbqJbdUU3vDN336Ic3avoFlLBcfUJfiWhTYrg6d8Bz2XGNi5UtKlckb/mqH H5gQ== MIME-Version: 1.0 X-Received: by 10.58.249.236 with SMTP id yx12mr9632482vec.25.1372684455855; Mon, 01 Jul 2013 06:14:15 -0700 (PDT) In-Reply-To: References: Date: Mon, 1 Jul 2013 23:14:15 +1000 Subject: Re: Stupid ways to spell simple code From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372684465 news.xs4all.nl 15916 [2001:888:2000:d::a6]:53543 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49559 On Mon, Jul 1, 2013 at 10:59 PM, Neil Cerutti wrote: > On 2013-06-30, Chris Angelico wrote: >> So, here's a challenge: Come up with something really simple, >> and write an insanely complicated - yet perfectly valid - way >> to achieve the same thing. Bonus points for horribly abusing >> Python's clean syntax in the process. >> >> Go on, do your worst! > > I've often thought it was redundant for Python to support 'if' > when it has dictionaries, cf the rationale for having no > 'switch'. > > valid_name = None > while not valid_name: > name = input("Enter your name: ") > valid_name = { > True: lambda: print("No name longer than 20 letters."), > False: lambda: True, > }[len(name) > 20]() > > Much better. Good! Good! But, waaaah. Waaaah. def get_name(): while True: name = input("Enter your name: ") yield { True: lambda: print("No name longer than 20 letters."), False: lambda: name, }[len(name) > 20]() name = next(filter(None,get_name())) ChrisA