[Python-ideas] Match statement brainstorm

M.-A. Lemburg mal at egenix.com
Tue May 24 04:27:04 EDT 2016


On 24.05.2016 08:08, Greg Ewing wrote:
>> On Mon, May 23, 2016 at 7:57 PM, Michael Selik
>> <michael.selik at gmail.com> wrote:
>>
>>> def demo(arg):
>>>    if p, q ?= arg.x, arg.y:                        # dict structure
>>>    elif x ?= arg.x and isinstance(x, int)          # assignment + guard
>>>    elif a, b, *_ ?= arg:                           # tuple structure
>>>    elif isinstance(arg, Mapping):                  # nothing new here
> 
> I'm unenthusiastic about this -- the above looks like
> an unreadable mess to me.

Seconded. It would be more readable to make the assignment
explicit:

  if (p, q = arg.x, arg.y):
      ...

but even then I find this error prone. Just think of the
common typo of writing "if x = 1: ..." instead of
"if x == 1: ...".

This version

  p, q = arg.x, arg.y
  if (p, q):
      ...

is just a bit more verbose, but comes without all the
problems of having to allow implicit assignment-in-expression
everywhere.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, May 24 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



More information about the Python-ideas mailing list