Parametric Polymorphism

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Mon Sep 26 05:59:08 EDT 2005


Kay Schluehr a écrit :
> Catalin Marinas wrote:
> 
>>Hi,
>>
>>Sorry if this was previously discussed but it's something I miss in
>>Python. I get around this using isinstance() but it would be cleaner
>>to have separate functions with the same name but different argument
>>types. I think the idea gets quite close to the Lisp/CLOS
>>implementation of methods.
> 
> 
> Guido himself addressed multimethods in his Artima blog:
> 
> http://www.artima.com/weblogs/viewpost.jsp?thread=101605
> 
> See also the subsequent discussion about subtyping problems. 
> 
> Kay
> 

Well, as said in the comments, what he propose is *not* working !
You cannot look for the type of the argument using a dictionnary of
definition types, it just doesn't work because of polymorphism.

Now suppose you have two classes A and B, B subclassing A.
If you define :

@method(A)
def myfct(f):
  do_something_with_f

Then, you want it to work with any object of type B ... but with either
Guido's or this implementation, it won't ! The problem is much more
complex and cannot be solved in constant (or even linear) time. What I
read concerning Lisp is they use a cache to optimize method resolution.

Pierre



More information about the Python-list mailing list