Stupid ways to spell simple code

Chris Angelico rosuav at gmail.com
Mon Jul 1 09:14:15 EDT 2013


On Mon, Jul 1, 2013 at 10:59 PM, Neil Cerutti <neilc at norwich.edu> wrote:
> On 2013-06-30, Chris Angelico <rosuav at gmail.com> 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



More information about the Python-list mailing list