Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alhabshi3k opened this issue Mar 29, 2021 · 2 comments
Closed

Comments

@alhabshi3k
Copy link

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.

@pxeger
Copy link
Contributor

pxeger commented Mar 29, 2021

This is not the place to suggest modifications to the specification in PEPs. In this case, an else clause was already considered and rejected; specifically in PEP 634's precursor PEP 622:

Add an else clause

We decided not to add an else clause for several reasons.

  • It is redundant, since we already have case _:
  • There will forever be confusion about the indentation level of the else: -- should it align with the list of cases or with the match keyword?
  • Completionist arguments like "every other statement has one" are false -- only those statements have an else clause where it adds new functionality.

link

In future, please go to python-ideas for changes to PEPs, but in this case you're likely to find it has been discussed in-depth and is unlikely to change, especially since PEP 634 was formally accepted.

@alhabshi3k
Copy link
Author

noted. thanks for the reply and information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants