overloading on type?

Burkhard Kloss bk at xk7.com
Wed Feb 14 06:20:16 EST 2001


One design problem I run across frequently in Python is how to nicely deal
with different types of arguments.  For example, writing a date class you'd
often have overloaded constructors in a statically typed language like C++:

class date
{
public:
    date (const date &);
    date (const string &);
    date (long);

in python, I catch myself writing things along the lines of

class date:
    def __init__ (self, arg):
        if type(arg) == types.InstanceType:
            ....
        elif type(arg) == types.IntType:
            ...

which works, but just seems so *wrong*.

Maybe I'm just having a conceptual block here as a result of my upbringing
(I'm a longtime and unrepentant C++ user and lover), but surely in a
language as nice as python there must be a better way to do this? :)
Overloading obviously doesn't fit into the conceptual framework of the
language.

Thoughts, anyone?

    Burkhard







More information about the Python-list mailing list