Why functional Python matters

Jp Calderone exarkun at intarweb.us
Wed Apr 16 02:45:37 CEST 2003


On Tue, Apr 15, 2003 at 11:04:06PM -0000, Dave Benjamin wrote:
> In article <3E9C9A91.8090209 at removethis.free.fr>, laotseu wrote:
> > Even for OO programmers, functionnal features in Python are IMHO a great 
> > plus, and BTW functionnal and OO paradigm does not have to conflict 
> > (that would be functionnal vs imperative and object vs procedural). CLOS 
> > is one of the great system objects out here, and it's been implemented 
> > on top of a functionnal language, so...
> 
> This is a good point. O'Caml comes to mind as well--a functional language
> with object support right out of the box. There is no reason the two
> methodologies need to be mutually exclusive, or step on each other's toes.
> 
> But, there is of course the risk of setting precedent that there are more
> than one ways to do it, and this is why I brought up Perl. I think that
> there is a subtle fear in the Python community of letting Python become too
> much like Perl, especially since both languages have similar application
> domains. There are other options besides TMTOWTDI and military rigor. This
> is precisely why I like Python, because it seems to be a good compromise
> between the two.
> 
> I think that list comprehensions bridge the gap somewhat between FP and OO
> methods, and this is one thing I really like them for. For instance:
> 
>   [obj.method(x) for x in data]
> 
> looks nicer to me than:
> 
>   map(lambda x: obj.method(x), data)
> 

  This reminds me of something I've long desired.  A "message" type.  I
think one might be implementable with descriptors, but I haven't gotten
around to figuring out just how yet.

  As many of us know, you can do this in 2.2:

    reduce(int.__add__, [1, 2, 3])

  Unfortunately, this breaks when ints get promoted to longs, because you
can't pass longs to int.__add__!  Similarly:

    map(ClassObj.method, [x, y, z])

  works, so long as x, y, and z are instances of ClassObj.  But if they are
instances of different types, all of which merely happen to implement
method, you're out of luck!  This is resolved with list comprehensions:

    [i.method(foo) for i in [x, y, z]]

  But what I'd like to see is a way to do:

    map(message("method", foo), [x, y, z])

  Of course, you could easily implement this as:

    def message(name, *args, **kwarg):
        return lambda o: getattr(o, name)(*args, **kwargs)

  But this is stupendously inefficient.  It'd be really great if there were
some built-in MessageType that could be used this way and also managed to
carry out the work in an efficient manner.

  And I think this is both a highly functional and a highly OO feature :)

  Jp

-- 
#!/bin/bash
( LIST=(~/.sigs/*.sig)
  cat ${LIST[$(($RANDOM % ${#LIST[*]}))]}
  echo -- $'\n' `uptime | sed -e 's/.*m//'` ) > ~/.signature
-- 
 up 26 days, 21:02, 4 users, load average: 0.25, 0.31, 0.63





More information about the Python-list mailing list