Overloaded Functions

Daniel da Silva ddasilva at umd.edu
Wed Jul 30 02:03:04 EDT 2008


With a little hacking, you might be able to do something like this:

@overload("f", (int, int, str))
def f1(x, y, z):
    pass

@overload("f", (str, str))
def f2(x, y):
    pass


The way I would typically do method overloading would be as follows
(this has been tested):

class Person:
    def __init__(*args):
        argTypes = tuple(map(type,args))

        methods = {
            (str,int) : Person.initAllInfo,
            (str,)    : Person.initOnlyName,
        }

        methods[argTypes[1:]](*args)

    # ........................

    def initAllInfo(self, name, age):
        self.name = name
        self.age  = age

    def initOnlyName(self, name):
        self.name = name
        self.age  = 100


With this overload-dictionary approach, it may be possible to
elegantly implement the overloading decorator I described at the top
of this email. I hoped this helped.

Daniel


On Tue, Jul 29, 2008 at 12:05 PM, Tim Henderson <tim.tadh at gmail.com> wrote:
> Yes i am aware of that but I want the code to be self documenting, so
> the intent is clear. I actually have an implementation using that
> style which you suggest. I would like cleaner style, like the one i
> suggested in my first post.
>
> Cheers
> Tim Henderson
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list