Overloading objects

Tom Lee tl_nntp at webcrumb.com
Tue Sep 2 11:58:29 CEST 2003


Batista, Facundo wrote:
> In others languages I can do (using sintaxis of Python):
> 
> 
> def myMethod (self, name):
>         self.name = name
> 
> def myMethod (self, name, age):
>         self.name = name
>         self.age = age
> 
> 
> If I'm not wrong, this is "Method Overloading".
> 
> I thought using defaults:
> 
> def myMethod (self, name, age=None):
>         self.name = name
>         if age not None:
>                 self.age = age
> 
> 
> but I'm concerned about the scalability of this.
> 
> What's the most pythonic way to do this? Using something like *args or 
> **args?
> 
> Thank you!
> 
> 
> .       Facundo

Scalability doesn't even come into this question - if you're really 
worried about performance, don't use Python.

Anyway, you have two or three choices:

1. Do it the way you're doing it.
2. Check parameter types at runtime using type() and the is keyword. e.g.
if type( somevar ) is int:
   self.do_int_stuff( somevar )
else:
   self.do_other_stuff( somevar )
3. Use similar names for similar methods. wxPython does this. e.g.
def Clean( self, something ):
   # common implementation

def CleanWithScourer( self, something ):
   # implementation using a Scourer instead of the default cleaning 
implement

Hope this helps.

- Tom L





More information about the Python-list mailing list