static method feature

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Nov 25 17:18:42 CET 2003


rashkatsa <rashkatsa at wanadoo.fr> wrote in
news:bpvu34$i7s$1 at news-reader3.wanadoo.fr: 

> 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):
>          ...

Not in any version of Python that I have ever seen. If you try this, then 
the second method will simply overwrite the first one. If you want 
polymorphism you need to look at the actual arguments passed to your method 
and adapt your processing accordingly.

In this case a less confusing thing to do is to make the optional msg 
parameter the last parameter and give it a default. This is how unittest 
implements assertEquals.
 
You can, of course, write a wrapper to dispatch methods based on argument 
types, but this tends to be messy and not usually needed.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list