[Python-ideas] ML Style Pattern Matching for Python
Eike Welk
eike.welk at gmx.net
Sun Dec 19 19:52:28 CET 2010
On Sunday 19.12.2010 02:03:43 Steven D'Aprano wrote:
> Eike Welk wrote:
> Your proposed syntax {| |} could be related to sets, or dicts, or both.
> Or it could be related to the | operator. If you don't know which, it's
> hard to guess. It looks more like a set than a dict:
>
> {a, b} # set
> {a: value, b: value} # dict
> {|a, b|} # looks like a set with extra symbols
>
> and there's probably nothing you can do with it in isolation from a full
> pattern match block.
I like this argument. This might lead to a design decision for the pattern
matching syntax: A pattern that matches an object should look closely like the
code that creates that object.
> > Class Creation
> > --------------
> >
> > Foo = class {| a, b |}
> What's so special about Foo that it needs special syntax just to make it
> easier? In any case, these days I'd suggest that's probably best written
>
> as a namedtuple:
> >>> from collections import namedtuple
> >>> Spam = namedtuple('Spam', 'a b c')
> >>> x = Spam(a=23, b=42, c=None)
> >>> x
>
> Spam(a=23, b=42, c=None)
Yes you are right. namedtuple is rely a perfect substitute for for Ocaml's
records. The only thing which it is missing, is optional type checking in the
constructor. Not only as a consistency test, but also as a terse form of
documentation.
>
> > Inheritance is not so important in the context of classes that have no
> > methods. It could be expressed with a new method ("inherits") of the
> > metaclass. Like this:
> >
> > Foo = class {| a, b |}.inherits(Bar, Baz)
> >
> > How one would create methods, and if it should be possible to create
> > method at all, needs to be discussed.
>
> I don't think it does :)
>
> I think you're falling into the trap of thinking that everything needs
> to be a one-liner. It doesn't.
Yes, in a "real" program you would want to document the attributes, and soon
the class definition would be no longer a one liner.
On the other hand: If the code is short (and readable), you can get an
overview much more easily.
My positive attitude towards this syntax comes from the only weakness that
Python IMHO has: You can't easily see which data attributes an instance has.
This information is hidden in __init__, and sometimes elsewhere. I think a
mechanism like slots should be the norm, and dynamism the exception.
>
> > Instance Creation
> > -----------------
> >
> > foo = {| a=1, b=2 |}:Foo
> >
> > This should be equivalent to:
> >
> > foo = Foo(a=1, b=2)
>
> Why do you think you need two ways of spelling Foo(a=1, b=2)? What's
> wrong with the Python syntax for it? It seems to me that you want the
> obfuscation of OCAML's syntax with the slowness of Python, a strange
> choice...
I would like to combine the nice aspects of Ocaml, with the unproblematic
nature of Python. In Ocaml I like the syntax for records, the match
expression, and some aspects of the file system. Most other things are IMHO
rather horrible.
Eike.
More information about the Python-ideas
mailing list