Method Overloading

Alex Martelli aleaxit at yahoo.com
Thu Aug 31 07:36:55 EDT 2000


"Quinn Dunkan" <quinn at zloty.ugcs.caltech.edu> wrote in message
news:slrn8qs9n8.dpe.quinn at zloty.ugcs.caltech.edu...
> On Wed, 30 Aug 2000 18:48:51 +1000, Nicholas Routledge
> <n.routledge at bigpond.com> wrote:
> >Damn.
> >its just that it would be so useful in this program! hehe.
> >
> >thanks anyway.
>
> Actually, python *does* support method overloading... but only if your
method
> happens to be an operator :)  Read in the lang ref about 'special method
> names'.

Even though it may be _called_ overloading, this has really
little or nothing to do with the "overloading" of Java or
C++ (Java, in fact, does not allow overloading of operators).

An "overloaded" function is one of which several versions
exist, with the same name; one of them is chosen by the
compiler, based on the arguments supplied, at compile-time.

Note well that this is strictly a static/compile-time issue:
having two methods called foo, with one and two arguments
respectively, is basically just syntax sugar for giving the
methods two different names such as foo1 and foo2.  All
arguments matter, in number and type, for the compile-time
choice of which method to use, ambiguity being an error.

The operator "overloading" that Python allows is more akin
to the concept of "virtual methods" in C++ (all Java methods
are virtual, though some may be "final" and thus not further
overridable): pick *at runtime* the right version of the
method, dispatching on *one* privileged argument.  It's not
a static issue, can't be solved at compile-time, just one
privileged argument matters for the dispatching.


Alex






More information about the Python-list mailing list