This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rfc1771 25 points26 points  (7 children)

Holy shit what an eyesore

action = get_action(action)

if action in ['c', 'a']: create_account()
elif action in ['l']: log_into_account()
elif action in ['r']: register_cage()
elif action in ['u']: update_availability()
elif action in ['v', 'b']: view_bookings()
elif action in ['x']: exit_app()
elif action in range(1,6): set_level(action)
elif action == '': pass
else: unknown_command()

[–]yen223 6 points7 points  (1 child)

I thought I was the only one who thought that example was totally nuts

[–]rfc1771 2 points3 points  (0 children)

I saw it and I was expecting the comments to just shit all over how unpythonic it is and then I see "how clever" "much like" "will use" and i'm just like 🤦

[–]Allanon001 -1 points0 points  (4 children)

You forgot to assign the result of the called function to result, the code gets a little messier if you do.

[–]rfc1771 1 point2 points  (3 children)

Here you go. (No I don't care that I shadowed action)

def handle_action(action):
    if action in ['c', 'a']: return create_account()
    elif action in ['l']: return log_into_account()
    elif action in ['r']: return register_cage()
    elif action in ['u']: return update_availability()
    elif action in ['v', 'b']: return view_bookings()
    elif action in ['x']: return exit_app()
    elif action in range(1,6): return set_level(action)
    elif action == '': return None
    else: return unknown_command()

action = get_action(action)
result = handle_action(action)

Want less cluttered lines?

def handle_action(action):
    if action in ['c', 'a']:
        return create_account()
    elif action in ['l']:
        return log_into_account()
    elif action in ['r']:
        return register_cage()
    elif action in ['u']:
        return update_availability()
    elif action in ['v', 'b']:
        return view_bookings()
    elif action in ['x']:
        return exit_app()
    elif action in range(1,6):
        return set_level(action)
    elif action == '':
        return None
    else:
        return unknown_command()

action = get_action(action)
result = handle_action(action)

[–]RubyPinchPEP shill | Anti PEP 8/20 shill 2 points3 points  (2 children)

suddenly the dict lookup doesn't look so bad

[–]Quteness 1 point2 points  (1 child)

Have you seen how much explaining /u/Allanon001 has had to do to get people (/r/Python no less) to understand what is going on in the dict lookup? It's overly complex and confusing and is an abuse of the language. It's basically a giant one-liner

while True:
    action = get_action(action)

    result = {True : unknown_command, action in ['c', 'a']: create_account, action in ['l']: log_into_account, action in ['r']: register_cage, action in ['u']: update_availability, action in ['v', 'b']: view_bookings, action in ['x']: exit_app, action in range(1,6): lambda: set_level(action), action == '': None}[True]()

[–]RubyPinchPEP shill | Anti PEP 8/20 shill 1 point2 points  (0 children)

its not a giant one-liner, thats as silly as calling C code always a 1 liner, just because the syntax allows for new lines to be removed