static method feature

R

rashkatsa

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.
 
D

Duncan Booth

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.
 
A

anton muhin

rashkatsa said:
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.
 
D

Diez B. Roggisch

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

Have you tried to make this run? It gives me this:

Traceback (most recent call last):
File "/tmp/test.py", line 19, in ?
ass.assertEquals(1,2)
TypeError: assertEquals() takes exactly 4 arguments (3 given)

when trying to invoke the first method.

Its not possible to have polymorphic methods in python at all - but you can
do this:

class Foo:
def bar(_, *args):
if len(args) == 2:
pass
elif len(args) == 3:
pass

This should also work with your static methods. Of course you can think of
more elaborated ways of dispatching, e.g. based on type(s) and so on. This
shall just give you the idea.

For keyword-args, you use **kwargs. And OTOH you can use similar syntax to
invoke a method with a list of args:

def baz(one, two, three):
pass

args = [1, 2, 3]

baz(*args)

Regards,

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,171
Messages
2,570,936
Members
47,472
Latest member
KarissaBor

Latest Threads

Top