does python support overloaded methods?(py newbie)

David Abrahams david.abrahams at rcn.com
Tue Jan 1 01:43:06 CET 2002


"Michael Kelly" <mkelly2002NOSPAM at earthlink.net> wrote in message
news:o2p13us48lu0r1o1m5e069maqfra1oluv3 at 4ax.com...

> >wanted to confirm this. Overloading method names(with parameters
> >belonging to different types) is possible in python??.

Not exactly. The Boost.Python library for creating extension modules in C++
supports overloading by linking all the overloads into a chain under the
control of a single callable object. Each overload in the chain is tried
until one succeeds.

You could do something similar in pure Python, but for it to work smoothly
you'd need to have some way to distinguish argument errors at the "outer
level". In other words, if

    * f1() has overloads a, b, and c
    * overload b calls some function f2()
    * f2's argument list doesn't match

you don't want to go on to try overload c, thinking that overload b's
argument list failed to match.

> Hmmmm, seems like this is a bigger deal in compiled
> languages since it's usually more hassle to deal with
> an indefinite number of arguments.  Scripting languages
> often have some catch-all syntax to get all the rest of
> the args.

It really has nothing to do with compiled vs. non-compiled languages.
Overloading gives us a mechanism to non-intrusively extend the semantics of
a construct, and it's the only clean way to deal with the problem of
multi-methods.

You /might/ say that it's a bigger deal in statically-typed languages, but I
think that's not even the case. Consider the trouble that Python has
handling coercions and binary operators cleanly.

-Dave







More information about the Python-list mailing list