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

Case clauses should be able to match list indexes #128313

Open
CodesMaha opened this issue Dec 28, 2024 · 5 comments
Open

Case clauses should be able to match list indexes #128313

CodesMaha opened this issue Dec 28, 2024 · 5 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) pending The issue will be closed if no feedback is provided topic-parser type-feature A feature request or enhancement

Comments

@CodesMaha
Copy link

CodesMaha commented Dec 28, 2024

Feature or enhancement

Proposal:

While list indexes can be written to match to other variables or constants, list indexes unfortunately currently cannot be cases to be matched with. Additionally, this dynamic sort of feature also already exists in JavaScript; although, it actually uses simpler value matching instead of languages like Python or C# that make use of structural pattern matching.

If addressed, Python developers will be able to simply edit a list to change case clauses without having to scroll through each and every one to find the one they'd like to change.

The use could potentially resemble the following example code…

lst = ['a', 'b', 'c'] # of options in this example

user_input = input(f'Input {lst[0]}, {lst[1]}, or {lst[2]}. \n: ').strip().lower()

match user_input:

    case lst[0]:
        print('\nFirst option chosen. ')
        ...

    case lst[1]:
        print('\nSecond option chosen. ')
        ...

    case lst[2]:
        print('\nThird option chosen. ')
        ...

    case _:
        print('\nInvalid input. Kindly try again.')
        ...

# shows a SyntaxError

However, as mentioned here later, someone suggested an edit in the above example code and provided the following code block — with the difference being the addition of ==, which can be is instead if appropriate…

lst = ['a', 'b', 'c'] # of options in this example

user_input = input(f'Input {lst[0]}, {lst[1]}, or {lst[2]}. \n: ').strip().lower()

match user_input:
    case == lst[0]:
        print('\nFirst option chosen. ')
        ...
    case == lst[1]:
        print('\nSecond option chosen. ')
        ...
    case == lst[2]:
        print('\nThird option chosen. ')
        ...
    case _:
        print('\nInvalid input. Kindly try again.')
        ...

They also highlighted how this addition had "an obvious (and accurate) mapping" to a version of the example code using if-else statements, for which they provided the following edition continuing after declarations…

if user_input == lst[0]:
    print('\nFirst option chosen. ')
    ...
elif user_input == lst[1]:
    print('\nSecond option chosen. ')
    ...
elif user_input == lst[2]:
    print('\nThird option chosen. ')
    ...
else:
    print('\nInvalid input. Kindly try again.')
    ...

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

LinkedIn Post: https://www.linkedin.com/posts/profile-of-maha-ijaz_serious-careerchoice-humour-activity-7278731541126524929-6jfI?utm_source=share

The image attached to the post describes how JavaScript already has this feature in its switch-case statements while the latest stable version of Python (3.13.x) does not.


Python discussion: https://discuss.python.org/t/case-clauses-should-be-able-to-match-list-indexes/75540

Opened after issue — sorry.

Summary of state as of December 30, 2024:

  • Two links presented, one to this same GitHub (GH) issue and another to the Stack Overflow (SO) question which is mentioned here afterwards as well.
  • A core CPython developer expressed approval and also suggested a subfeature from PEP 642, which that same person authored. The subfeature in question allows for == and is to be used as prefix operations to replace implicit value patterns in this case.

Old but related SO question:
https://stackoverflow.com/questions/66159432/how-to-use-values-stored-in-variables-as-case-patterns

In this, one account even said,

that's been proposed for Python (and rejected) twice before …

It's interesting to see how many times this exact topic has been brought up or revisited.

@CodesMaha CodesMaha added the type-feature A feature request or enhancement label Dec 28, 2024
@ZeroIntensity
Copy link
Member

Please bring this up on DPO first, but in general, this isn't what match is supposed to be for--you should just use an if here (or better yet, a dictionary mapping of choices to functions). match is for structural pattern matching, and this isn't structural nor a pattern. You might want to take a look at PEP-636 for the use cases.

@ZeroIntensity ZeroIntensity added the pending The issue will be closed if no feedback is provided label Dec 28, 2024
@picnixz
Copy link
Contributor

picnixz commented Dec 28, 2024

In this case, you can use a StrEnum. The issue with lst[0] is that the latter is not a constant (or more precisely, it cannot be understood correctly in a structural pattern matching context)

@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser labels Dec 28, 2024
@CodesMaha
Copy link
Author

Please bring this up on DPO first,

Of course, done :)
Sorry for not doing so earlier.

No one has replied yet, though, so I'm not sure if the general consensus is with or against this on there.

you should just use an if here

Yes, that is what I did in my personal Python code a few days ago when I discovered this before researching it.
Thank you for your suggestion nonetheless.

match is for structural pattern matching, and this isn't structural nor a pattern. You might want to take a look at PEP-636 for the use cases.

Sure, I can definitely do that again if you insist :)

@ZeroIntensity
Copy link
Member

No one has replied yet, though, so I'm not sure if the general consensus is with or against this on there.

A lot of people (especially core devs) are off for the holidays at the moment, so I wouldn't expect speedy replies.

@CodesMaha
Copy link
Author

CodesMaha commented Dec 28, 2024

The issue with lst[0] is that the latter is not a constant (or more precisely, it cannot be understood correctly in a structural pattern matching context)

Yes, and I saw that the reason it worked for JavaScript is because it used simpler value matching, whereas languages like Python or C# use the aforementioned structural pattern matching.

I'm not sure if this issue should be closed prematurely then, though.

In this case, you can use a StrEnum

I agree; that is indeed a good alternative for the meanwhile for anyone encountering this.

Thank you for your comment :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) pending The issue will be closed if no feedback is provided topic-parser type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants