static method feature

rashkatsa rashkatsa at wanadoo.fr
Tue Nov 25 10:57:20 EST 2003


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.

with no static methods, it is already possible in python :

class Assert:
     def assertEquals(self,expected,actual):
         ...
     def assertEquals(self,msg,expected,actual):
         ...

but as soon as you use static :
class Assert:
     def assertEquals(self,expected,actual):
         ...
     assertEquals=staticmethod(assertEquals)
     def assertEquals(self,msg,expected,actual):
         ...
     # here you can't declare agin the same
     # assignment : assertEquals=staticmethod(assertEquals)
     # because they only differ from the number of parameters

several design solutions :

1) It could be possible to extend staticmethod(function) to 
staticmethod(function, arity) and resolve during call with the len of 
arguments pass to the function (we could imagine that default parameter
are not allowed : it is the price for the speed :))
2) You could add a new key in the language (defstatic for example) like 
in the following :

class Assert:
     defstatic assertEquals(expected,actual):
         ...
     defstatic assertEquals(msg,expected,actual):
         ...
but you will now have two keywords for methods definition
3) The best seems to add a method attribute like static before def keyword.

what is your opinion ?

regards,

rashkatsa.





More information about the Python-list mailing list