Does python support multi prototype.

xtian xtian at toysinabag.com
Mon Aug 9 00:41:06 EDT 2004


Matteo Dell'Amico <della at toglimi.linux.it> wrote in message news:<w54Qc.29647$OH4.805468 at twister1.libero.it>...
> angel wrote:
> > Java and cpp support one function name multi function prototype.
>  [...]
> > Does python support it?
> 
> Not natively, but - if you really need them - there are modules that 
> make them possible.
> 
> Have a look at 
> http://gnosis.cx/publish/programming/charming_python_b12.html , and 
> maybe at 
> http://codespeak.net/pypy/index.cgi?doc/objspace/multimethod.html too.

One thing to note is that these Lisp-ish multimethods are more
powerful than Java- or C#-style method overloading, because the method
dispatch is done at runtime using the actual types of the arguments.
In Java and C# overload selection is done at compile time, so if
you've got, frex:

public class Thing {
    public void DoSomething(Integer val) {
        ...
    }

    public void DoSomething(String val) {
        ...
    }

    public void DoSomething(Object val) {
        ...
    }
}


[and then]

Object o = "fish";
Thing t = new Thing();
t.DoSomething(o);

... you'll always get the object version of DoSomething called,
because the declared type of o is Object.

Which always annoyed me.

xtian



More information about the Python-list mailing list