function overloading

Karl Scalet news at yebu.de
Thu Jul 3 04:35:32 EDT 2003


Alex Martelli schrieb:
> 
> We don't (bind several different objects to the same name in a given
> scope).  If you're keen on being able to call setPos(x,y) as well as
> setPos(pos) there are several ways you can achieve this effect -- e.g.
> 
> def setPos(x,y):
>   "whatever"
> 
> setPos_2args = setPos
> 

what is the benefit of this "renaming" over just
name the dispatch-function setPos_2args directly:

def setPos_2args(x,y):
     "whatever"

 > ....
> 
> def setPos(*args):
>     if len(args)==2: return setPos_2args(*args)
>     if len(args)>1: raise TypeError
>     if type(args[0]) == postype: return setPos_postype(*args)
>     return setPos_anyother(*args)
> ...

Karl





More information about the Python-list mailing list