[Python-ideas] explicitation lines in python ?

Carl M. Johnson cmjohnson.mailinglist at gmail.com
Mon Jul 12 08:50:28 CEST 2010


One more quick thought about the advantage of a where-clause. Often
times, there is thought of creating the equivalent of Haskell-style
pattern matching using decorators. For example PJE has worked on
creating "generic functions." One problem with using a decorator for
this is that for use cases more complicated than just matching on
type, the matcher itself needs to be a function that looks at the
arguments then returns true or false based on whether they match a
pattern. So, a proper decorator would need to take *two* functions,
one to do the matching and one to actually be the body of the
function. You can do this to some extent with lambdas or decorating
and redecorating, but it quickly becomes a little tedious. With a
where-clause one might instead write:

fib = base(cond, action) where:
    def cond(n): return n in (0, 1)
    def action(n):
        return 1

fib.add_match(cond, action) where:
    def cond(n):
        return isinstance(n, int) and n > 1
    def action(n):
        return n + fib(n - 1)

And also for the property decorator. GvR made up a nice way of
redecorating with properties, so this is a moot point now, but if we
had had a where-clause before that, we could have instead written:

myprop = property(getter, setter, deleter) where:
    def getter(self):
        etc.
    etc.

OK, that’s as much advocacy as I feel like doing. See you all again in
6 months, when something like this is proposed again. ;-)

-- Carl



More information about the Python-ideas mailing list