Method overloading?

Grant Edwards grante at visi.com
Thu Feb 15 00:04:17 EST 2007


On 2007-02-15, placid <Bulkan at gmail.com> wrote:

> Is it possible to be able to do the following in Python?
>
> class Test:
>     def __init__(self):
>         pass
>
>     def puts(self, str):
>         print str
>
>     def puts(self, str,str2):
>         print str,str2
>
> if __name__ == "__main__":
>     t = Test()
>     t.puts("hi")
>     t.puts("hi","hello")

You tell us: what happened when you tried it?

And then what happens when you do this?

class Test:
    def __init__(self):
        pass
    
    def puts(self, *args):
        print ' '.join(args)

if __name__ == "__main__":
    t = Test()
    t.puts("hi")
    t.puts("hi","hello")

Now an exercise for the gentle reader: change the puts method
so that this call works:

    t.puts("hi",1,3.45)
    
-- 
Grant Edwards                   grante             Yow!  Yow! I'm imagining
                                  at               a surfer van filled with
                               visi.com            soy sauce!



More information about the Python-list mailing list