Stupid ways to spell simple code

Neil Cerutti neilc at norwich.edu
Mon Jul 1 08:59:16 EDT 2013


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.

-- 
Neil Cerutti



More information about the Python-list mailing list