static method feature

anton muhin antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Tue Nov 25 11:32:58 EST 2003


rashkatsa wrote:
> Hi all,
> 
> do you know why python development team decided to forbid polymorphism 
> for static methods ? As you can do it in another languages (Java,...) it 
> could be very handy if you can create utility classes with static 
> methods that could be differentiate from the number of parameters.
It's not polymorphism, but overloading

> 
> with no static methods, it is already possible in python :
> 
> class Assert:
>     def assertEquals(self,expected,actual):
>         ...
>     def assertEquals(self,msg,expected,actual):
No, you just *redefine* assertEquals method

Try Assert().assertEquals('expected', 'actual')

and Python'd complain:
TypeError: assertEquals() takes exactly 4 arguments (3 given)

[skipped]

Python doesn't support overloading (search the group for more info on it).

It can be emulated to some extened with default values and *args, **args

E.g.:

def foo(*args):
     if len(args) == 3:
          return foo3(*args)
     else:
          return default_foo(*args)

A nice example of dispatch on passed parameters is multimethod.py (you 
can google for it).

regards,
anton.





More information about the Python-list mailing list