On Wed, Jun 24, 2020 at 3:49 PM Ethan Furman <ethan@stoneleaf.us> wrote:
On 06/23/2020 10:31 PM, Chris Angelico wrote:
On Wed, Jun 24, 2020 at 3:21 PM Ethan Furman wrote:
On 06/23/2020 09:01 AM, PEP 622 wrote:
from enum import Enum
class Color(Enum): BLACK = 1 RED = 2
BLACK = 1 RED = 2
match color: case .BLACK | Color.BLACK: print("Black suits every color") case BLACK: # This will just assign a new value to BLACK. ...
As others have noted, the leading dot to disambiguate between a name assignment and a value check is going to be a problem. I think it's also unnecessary because instead of
case BLACK: blahblah()
we can do
case _: # look ma! BLACK is just "color"! BLACK = color # if you really want it bound to another name
In other words, the PEP is currently building in two ways to do the same thing -- make a default case. One of those ways is going to be a pain; the other, once renamed to "else", will be perfect! :-) As a bonus, no special casing for leading dots.
But what if that's composed into something else?
class Room(Enum): LIBRARY = 1 BILLIARD_ROOM = 2 ...
match accusation: case (Color.SCARLETT, Room.BILLIARD_ROOM): print("Correct") case (Color.SCARLETT, _): print("Not there!") case (_, Room.BILLIARD_ROOM): print("Wrong person!") case (_, _): print("Nope. Just nope.")
Without the dots, there's no way to tell whether you're matching specific values in the tuple, or matching by length alone and then capturing. You can't use the 'else' keyword for a partial match.
Well, your example isn't using leading dots like `.BLACK` is, and your example isn't using `case _` as a final catch-all, and your example isn't using "case some_name_here" as an always True match. In other words, your example isn't talking about what I'm talking about. ;-)
But it IS using "_" as a catch-all. The simple "case _:" case is using _ the same way that "case (Color.SCARLETT, _):" is. ChrisA