-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
Comments
Please bring this up on DPO first, but in general, this isn't what |
In this case, you can use a |
Of course, done :) No one has replied yet, though, so I'm not sure if the general consensus is with or against this on there.
Yes, that is what I did in my personal Python code a few days ago when I discovered this before researching it.
Sure, I can definitely do that again if you insist :) |
A lot of people (especially core devs) are off for the holidays at the moment, so I wouldn't expect speedy replies. |
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.
I agree; that is indeed a good alternative for the meanwhile for anyone encountering this. Thank you for your comment :) |
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…
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 beis
instead if appropriate…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…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:
==
andis
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,
It's interesting to see how many times this exact topic has been brought up or revisited.
The text was updated successfully, but these errors were encountered: