On Wed, Jun 2, 2021 at 1:17 PM Julia Schmidt <accountearns...@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
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/7HXJSAQCGI3Y2N5ERQNF4LAR4J24MDX4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to