Method overloading?

Troy Melhase troy.melhase at gmail.com
Thu Feb 15 03:32:09 EST 2007


On 14 Feb 2007 20:54:31 -0800, placid <Bulkan at gmail.com> wrote:
> class Test:
>     def __init__(self):
>         pass
>
>     def puts(self, str):
>         print str
>
>     def puts(self, str,str2):
>         print str,str2

you might look into the overloading module and its decorator.  source
is in the sandbox:

http://svn.python.org/view/sandbox/trunk/overload/overloading.py

using it, you could re-write your example as:

#
from overloading import overloaded

class Test(object):

    @overloaded
    def puts(self, S):
        print S

   @puts.register(object, str, str)
    def puts_X(self, S, S2):
        print S, S2

two things to note.  first, i changed your class to derive from
object.  I don't know if that's required, but i suspect it is.

second, i changed your argument names.  the argument names in your
example shadow built-in names.  you shouldn't do that in any case, but
it could become especially confusing using the overloaded decorator,
which relies on argument type to select the correct method.



More information about the Python-list mailing list