[Python] Performant Python elif handling

Discussão em 'Python' iniciado por Stack, Maio 11, 2025.

  1. Stack

    Stack Membro Participativo

    Currently, I have a Discord application which in turn has a custom Command-Line Interface. It uses an elif approach to sort through commands, however I have a large number of commands to handle and it becomes a necessity to clean it.

    This doesn't exactly impact my usage of the application, but I feel as though it's messy and overtime may result in slower execution speeds.

    What would be the best approach to go about this?

    async def Handle(self, Cmd: str):
    if not Cmd: return
    LastCommand = Cmd
    if Cmd == "quit":
    print("| Disconnecting application.")
    await self.close()
    elif Cmd == "prev":
    if not LastCommand: return
    if LastCommand == "prev": print("Cannot execute 'prev' command as a previous command.")
    print(f"| Executing previous command '{LastCommand}'")
    await self.Handle(LastCommand)
    # Other commands using elif
    else:
    print(f"| Unknown command '{Cmd}'")

    Continue reading...

Compartilhe esta Página