Method overloading?

placid Bulkan at gmail.com
Thu Feb 15 00:12:39 EST 2007


On Feb 15, 4:04 pm, Grant Edwards <gra... at visi.com> wrote:
> On 2007-02-15, placid <Bul... 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?

Well, when i run it i get this error "puts() takes exactly 3 arguments
(2 given)" which means that the second a time i try to define the
puts() method "overwrites" the first one

>
> 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")

but this isn't overloading.

>
> 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