The Pattern Matching Wildcard Is A Bad Idea
From https://docs.python.org/3.10/whatsnew/3.10.html: def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the Internet" The wildcard idea looks just wrong and confusing. What if I want to use it as a variable and match against it like _ = 504? This is how it should be done: def http_error(status): _ = 504 match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Gateway Timeout" else: return "Something's wrong with the Internet"
Hi Julia, and welcome! On Wed, Jun 02, 2021 at 05:16:29AM +0200, Julia Schmidt wrote:
The wildcard idea looks just wrong and confusing. What if I want to use it as a variable and match against it like _ = 504?
Don't do that. There is a very strong convention in Python that a single underscore is to be used for two purposes (and a third in the interactive interpreter only): (1) For "don't care" variables; anything that you don't care about and aren't intending to use should be named `_`. first, *_, last = sequence This tells me that I am only using the first and last items, everything else is ignored. (2) For internationalisation. This is a convention that comes from other languages, but it is so widespread that we're kinda stuck with it. print(_("Try again.")) By convention, the underscore function is used to translate messages into the current location's language. (3) And in the interactive interpreter, underscore is a special variable which captures the previous result. This doesn't work in scripts. >>> 5 + 4 9 >>> _*10 90 So aside from localisation, the Pythonic convention is that a name consisting only of an underscore alone is something to be ignored. There is no shortage of names. (However, good, informative, self- descriptive names are harder to come by.) If you really must name something with an underscore, instead of giving it an informative name, you can just use two underscores. Three or more would be better, to make it more visually distinctive. ____ = 504 # Why not HTTP_GATEWAY_TIMEOUT ??? But for what it's worth, if I were doing a code review and saw somebody using underscore-only names for anything but "ignore this", I would likely be asking some pointed questions about why it was using such obfuscated names. -- Steve
On Wed, Jun 2, 2021 at 1:17 PM Julia Schmidt <accountearnstar@gmail.com> wrote:
From https://docs.python.org/3.10/whatsnew/3.10.html:
def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the Internet"
The wildcard idea looks just wrong and confusing. What if I want to use it as a variable and match against it like _ = 504? This is how it should be done:
def http_error(status): _ = 504 match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Gateway Timeout" else: return "Something's wrong with the Internet"
What you're suggesting wouldn't work anyway. The underscore is only very minorly special in a match statement. *Any* simple name will function as a catch-all; the only thing that's special about the underscore is that it won't bind to that name. match status: case 400: ... case 404: ... case 418: ... case other: return "HTTP error %d" % other The same is true inside any sort of nested match. You can "case [x, y]:" and it'll take any two values and bind to x and y, or you can "case [x, _]:" to take any two values, bind the first to x, and ignore the second. To match against non-constants, normally you'd want to use an enumeration or somesuch. You can read more about pattern matching in PEP 636: https://www.python.org/dev/peps/pep-0636/ ChrisA
This was discussed/litigated/holy wars fought over at extreme length, I suggest you peruse the email archives searching for PEP 622 or 634 and also this might be a helpful jumping off point: https://github.com/gvanrossum/patma/issues. On Wed, 2 Jun 2021 at 04:18, Julia Schmidt <accountearnstar@gmail.com> wrote:
From https://docs.python.org/3.10/whatsnew/3.10.html:
def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the Internet"
The wildcard idea looks just wrong and confusing. What if I want to use it as a variable and match against it like _ = 504? This is how it should be done:
def http_error(status): _ = 504 match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Gateway Timeout" else: return "Something's wrong with the Internet"
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/EJE2T2... Code of Conduct: http://python.org/psf/codeofconduct/
participants (4)
-
Chris Angelico
-
Henk-Jaap Wagenaar
-
Julia Schmidt
-
Steven D'Aprano