Skip to content

suggestion to small improvement of PEP 634 -- Structural Pattern Matching #1898

@alhabshi3k

Description

@alhabshi3k

Firstly, congratulation on PEP 634, I just want to suggest an enhancement for the "match" statement.

  • When the matching against cases of pattern, there are some situations when there is no match.
  • Using a cluster of "else" make it more readable compared with using a wildcard case.
  • It's likewise in cases of the "if", "for", "try" statements, when conditions are not matching or when elements in a collection are not found, having the cluster of "else" in the above-mentioned statements make the code syntaxial readable to express what the Python program should.

For example , taken from PEP 636 , instead of :

match command.split():
    case ["go", direction] if direction in current_room.exits:
        current_room = current_room.neighbor(direction)
    case ["go", _]:
        print("Sorry, you can't go that way")

the programmer have express a possible situation as :

match command.split():
    case ["go", direction] if direction in current_room.exits:
        current_room = current_room.neighbor(direction)
    case ["go", _]:
        print("Sorry, you can't go that way")
    else : 
        print( f"Sorry, I don't understand what you mean by {command}.")

I hope you got my point.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions