[Python-ideas] Match statement brainstorm

Ethan Furman ethan at stoneleaf.us
Wed May 25 05:17:51 EDT 2016


On 05/25/2016 02:04 AM, Franklin? Lee wrote:
> On Wed, May 25, 2016 at 1:52 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> Using the running demo:
>>
>>      def demo(arg):
>>          given arg:
>>              case x, y, *_: # Tuple matching (implicit name binding)
>>                  ...
>>              case (.x, .y) as p, q: # Attribute matching
>>                  ...
>>              case (["x"], ["y"]) as p, q: # Item matching
>>                  ...
>>              case (.x) as p and isinstance(p, int): # Match + condition
>>                  ...
>>              case if isinstance(arg, int): # Condition only
>>                  ...
>>              else: # Default
>>                  ...
>>
>> The other key change there is introducing "as" to the individual cases
>> in order to be able to separate the match pattern definition from the
>> local name binding.
>
> I still don't like that `case THING` is a pattern, rather than a value
> to test against. Here's my modifications with "as", attributes, and
>
>       def demo(arg):
>          given arg:
>              case as x, y, *_: # Tuple matching (implicit name binding)
>                  ...
>              case as object(x=p, y=q, **_): # Attribute matching
>                  ...
>              case as {'x': p, 'y', q, **_}: # Item matching
>                  ...
>              case as object(x=p, **_) and isinstance(p, int): # Match + condition
>                  ...
>              case if isinstance(arg, int): # Condition only
>                  ...
>              else: # Default
>                  ...

'case' is the signal word, 'as' is the seperator word -- but it should 
be separating user stuff, not keyword and all the user stuff clumped 
together.

`case blah blah as blah blah reads much better to me, and clearly says 
"the stuff between 'case' and 'as' will now be known as the stuff 
between 'as' and ':'".

--
~Ethan~


More information about the Python-ideas mailing list