[Python-Dev] Re: PEP 642 v3: Explicit patterns for structured pattern matching

2021年1月03日 08:37:45 -0800

On Sun, 3 Jan 2021 16:19:01 +0000
Barry Scott <[email protected]> wrote:
> > 
> > I’ll also quote the example match statement from the PEP abstract,
> > which extracts "host" and "port" details from a 2 item sequence, a
> > mapping with "host" and "port" keys, any object with "host" and "port"
> > attributes, or a "host:port" string, treating the "port" as optional
> > in the latter three cases:
> > 
> > port = DEFAULT_PORT
> > match expr:
> > case [as host, as port]:
> > pass
> > case {"host" as host, "port" as port}:
> > pass
> > case {"host" as host}:
> > pass
> > case object{.host as host, .port as port}:
> > pass
> > case object{.host as host}:
> > pass
> > case str{} as addr:
> > host, __, optional_port = addr.partition(":")
> > if optional_port:
> > port = optional_port
> > case __ as m:
> > raise TypeError(f"Unknown address format: {m!r:.200}")
> > port = int(port) 
> 
> I read the above and believe I know what it meant without needing to read the 
> PEP in detail.
> I like that a lot.
+1. Unlike the other PEP, there is no confusion with regular Python
syntax such as function calls.
Regards
Antoine.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/23DGXSXJ32DMJMZIETSEWGYDX2RNIR3W/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to