473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static method feature

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.

Jul 18 '05 #1
3 2320
rashkatsa <ra*******@wanadoo.fr> wrote in
news:bp**********@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 du****@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?
Jul 18 '05 #2
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.

Jul 18 '05 #3
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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Hal Vaughan | last post by:
In the app I'm working on, there is a need, from all classes, to get configuration information. Much of that info is stored in single line files (which may be put in the Preferences structure in...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
19
by: Mike Ruane-Torr | last post by:
Why can't I have a static abstract method in C#? My intention is to have a class-level method that returns a string to supply information about inherited classes, and it is natural to make this...
2
by: Tony Maresca | last post by:
Don't know if there are any Delphi/Object Pascal converts here, but being one, I sorely miss the feature that permits class (e.g., static) methods to be virtual and be overridden in derived...
6
by: gg9h0st | last post by:
i really wander what makes static method special? in fact i can access a non static method statically using '::' class aclass { function anonstatic() { echo 'non static'; } static...
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
49
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current...
0
by: Joe Strout | last post by:
Hi Luis, A static variable IS encapsulation. Encapsulation happens at many levels: module, class, instance, and (in languages that support it) method. A static local variable is simply the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.