Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Alternative wildcard #145

Open
gvanrossum opened this issue Aug 26, 2020 · 8 comments
Open

Alternative wildcard #145

gvanrossum opened this issue Aug 26, 2020 · 8 comments
Labels
fully pepped Issues that have been fully documented in the PEP rejected A rejected idea sc-feedback Issues raised in the steering committee feedback

Comments

@gvanrossum
Copy link
Owner

gvanrossum commented Aug 26, 2020

Several SC members (at least 3) would prefer a different wildcard symbol than _, and their preferred symbol is ?.

Some of the reasons heard include:

  • Don't buy the argument that this is used in so many other languages
  • Prefer else: instead of case _: (or case ?: for that matter)
  • Apparently people use this commonly: _ = something(); print(_), which would make them believe that case _: print(_) would be valid
  • Confusion with other uses of _ like an alias for gettext or the last value printed by the REPL
  • (UPDATE:) It would be another "exception to the rules"/"special case"

Honestly, while I still prefer _, I could live with ? if that was needed to get the SC's approval.

UPDATE: In defense of copying from other languages: let's not unnecessarily invent where we can easily copy.

@gvanrossum gvanrossum added the sc-feedback Issues raised in the steering committee feedback label Aug 26, 2020
@Tobias-Kohn
Copy link
Collaborator

I fully agree with all of it. And let's be clear: this one is not about the question what is objectively the best option, but rather about personal preferences.

  • Yes, the syntax used in other languages is not necessarily a strong argument, but the "Python does it differently anyway" that I have seen brought forward on the mailing list a couple of times does not offer any valuable guidance at all. With all the emphasis on readability and teachability in general, I find it strange that a consensus reached by virtually all other languages is dismissed so easily.
  • Although the else:-issue has indeed been brought forward in connection with the wildcard character, it is actually completely unrelated to the question how we "spell" the wildcard.
  • I find this one particularly weak for two reasons. First, I honestly do not see how such a use of _ could be good coding practise in any way. Secondly, this is probably one of the places that I would flag as "weasel words" on Wikipedia and ask for clarification: who?, when?, why? And why is this more important than the also common pattern of _, _, x, _ = something()?
  • There is one point that hardly anyone seems to consider during this entire discussion: there is absolutely no requirement to use wildcards in pattern matching in the first place. While wildcards can help make your life easier, you can actually write all patterns entirely without it. So, if someone is concerned about confusion with other uses of _: just use other throw-away variables instead.

I cannot detect any convincing or really grounded argument in any of the points brought forward against the use of _ as a wildcard until now. That's why I am convinced this really is about personal preference and a vague sense of "that looks weird" rather than actual reasons.

Finally, concerning other languages: perhaps we should acknowledge that Python does not exist in empty space. Virtually everyone with at least intermediate coding skills will at the very least have seen other programming languages, maybe even used one. That basically all mainstream programming languages use a common vocabulary including if, while, for, etc. really helps you pick up a new language fast, and, moreover, to read and translate code written in another language. Hence, I think that a common vocabulary and similarities in syntax do matter. By itself, it is not strong enough to work as a universal design tenet, but I think it short-sighted to dismiss it so quickly.

@gvanrossum
Copy link
Owner Author

Thanks for your points! I think we can explain this one to the SC in person with this.

PS: There is one concern with not having wildcards at all: we have a rule disallowing case (x, x) and that rule would also disallow case (_, _, 1). Would we consider allowing case (x, x) just so we can make _ a wildcard by convention instead of by specification?

@Tobias-Kohn
Copy link
Collaborator

It's that thing of being caught between a rock and a hard place, I am afraid: if we allow case (x, x):—which will probably not mean what most people are expecting—, we have another one of those foot guns and to deal with that.

Don't get me wrong: if allowing case (x, x): solves all our problems, let's go with it and make it so. It's one of these things where I feel allowing it is the "wrong way", but it does not touch on the core issues enough to be a major problem as far as I can see.

@dmoisset
Copy link
Collaborator

For reference, an additional reason given for going against _ is that it would be another "exception to the rules"/"special case"

@JelleZijlstra
Copy link

It's worth noting that x, x = 1, 2 is legal, and as far as I can tell it's not been a major problem. flake8 doesn't even lint against it, but pylint does (it's redeclared-assigned-name/W0128). So maybe allowing the analogous issue in patterns won't cause major problems either.

(Sorry for intruding here; let me know if you'd rather I stop.)

@jeanas
Copy link

jeanas commented Aug 27, 2020

I am concerned that "case x, ?:" might look like there can be one or two elements.

(Ditto.)

@gvanrossum gvanrossum added fully pepped Issues that have been fully documented in the PEP rejected A rejected idea labels Sep 16, 2020
@gvanrossum
Copy link
Owner Author

In the SC-VC we ended up deciding to keep the existing _ symbol.

@gvanrossum gvanrossum added needs more pep An issue which needs to be documented in the PEP and removed rejected A rejected idea labels Sep 16, 2020
@gvanrossum gvanrossum added rejected A rejected idea and removed needs more pep An issue which needs to be documented in the PEP labels Oct 20, 2020
@gvanrossum
Copy link
Owner Author

I think I meant to label this as rejected (we're keeping _ in our proposal) and I added some language about this to PEP 635, so I think it's fully pepped.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
fully pepped Issues that have been fully documented in the PEP rejected A rejected idea sc-feedback Issues raised in the steering committee feedback
Projects
None yet
Development

No branches or pull requests

5 participants