[Python] Continue to match next case after match?

Discussão em 'Python' iniciado por Stack, Junho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have a match case that I want to execute even if a previous statement already matched.

    Here's what I currently have:

    key: Literal['all', 'a', 'b'] = 'a'

    def do_a():
    pass
    def do_b():
    pass

    match key:
    case 'a':
    do_a()
    case 'b':
    do_b()
    case 'all':
    do_a()
    do_b()


    Is there any way to simplify the code so I can remove the case 'all'?

    Something like

    match key:
    case 'a' | 'all':
    do_a()
    case 'b' | 'all':
    do_b()

    Continue reading...

Compartilhe esta Página