473,408 Members | 2,839 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,408 software developers and data experts.

Reuse base-class implementation of classmethod?

Hello,

what's the magic needed to reuse the base-class implementation of a
classmethod?

class A(object):
@classmethod
def foo(cls, a,b):
# do something
pass

class B(A):
@classmethod
def foo(cls, a, b):
A.foo(cls, a, b) # WRONG!

I need to call the base-class classmethod to reuse its implementation, but I'd
like to pass the derived class as first argument.
--
Giovanni Bajo
Nov 1 '05 #1
3 2009

Giovanni Bajo wrote:
Hello,

what's the magic needed to reuse the base-class implementation of a
classmethod?

class A(object):
@classmethod
def foo(cls, a,b):
# do something
pass

class B(A):
@classmethod
def foo(cls, a, b):
A.foo(cls, a, b) # WRONG!

I need to call the base-class classmethod to reuse its implementation, but I'd
like to pass the derived class as first argument.
--
Giovanni Bajo


See the super object. In your case, it can be used like this:

class B(A):
@classmethod
def foo(cls, a, b):
super(B, cls).foo(a, b)

This all assumes you want to modify the behavior of foo in a subclass.
If not, of course, you don't need to override it at all.

-- David

Nov 1 '05 #2
On Tue, 01 Nov 2005 00:24:37 GMT, "Giovanni Bajo" <no***@sorry.com> wrote:
Hello,

what's the magic needed to reuse the base-class implementation of a
classmethod?

class A(object):
@classmethod
def foo(cls, a,b):
# do something
pass

class B(A):
@classmethod
def foo(cls, a, b):
A.foo(cls, a, b) # WRONG!

I need to call the base-class classmethod to reuse its implementation, but I'd
like to pass the derived class as first argument.
--
Giovanni Bajo

Maybe define a funny-class-method decorator?
(nothing below tested beyond what you see ;-)
def funnycm(m): ... return property(lambda cls: m.__get__(type(cls), type(type(cls))))
... class A(object): ... @funnycm
... def foo(cls, a, b):
... print cls, a, b # do something
... class B(A): ... pass # just inherit
... a=A()
a.foo(1,2) <class '__main__.A'> 1 2 b=B()
b.foo(1,2) <class '__main__.B'> 1 2

Or more directly, a custom descriptor (with dynamic method replacement for good measure ;-)
class funnycm(object): ... def __init__(self, f): self._f = f
... def __get__(self, inst, cls=None):
... return self._f.__get__(inst is None and cls or type(inst))
... def __set__(self, inst, m): self._f = m # replace method
... class A(object): ... @funnycm
... def foo(cls, a, b):
... print cls, a, b # do something
... class B(A): ... pass # just inherit
... a=A()
a.foo(1,2) <class '__main__.A'> 1 2 b=B()
b.foo(1,2) <class '__main__.B'> 1 2 A.foo(3,4) <class '__main__.A'> 3 4 B.foo(3,4) <class '__main__.B'> 3 4 a.foo = lambda cls, *args: repr(args)
a.foo('what','now','then?') "('what', 'now', 'then?')" b.foo('eh?') "('eh?',)" B.foo()

'()'

Vary to taste ;-)

Regards,
Bengt Richter
Nov 1 '05 #3
David Wahler wrote:
what's the magic needed to reuse the base-class implementation of a
classmethod?

class A(object):
@classmethod
def foo(cls, a,b):
# do something
pass

class B(A):
@classmethod
def foo(cls, a, b):
A.foo(cls, a, b) # WRONG!

I need to call the base-class classmethod to reuse its
implementation, but I'd like to pass the derived class as first
argument. --
Giovanni Bajo


See the super object. In your case, it can be used like this:

class B(A):
@classmethod
def foo(cls, a, b):
super(B, cls).foo(a, b)

Ah thanks, I did not realize that super() was the only way of doing the right
thing here!
--
Giovanni Bajo
Nov 1 '05 #4

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

Similar topics

3
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C? How is "code reuse" realized in C++? By...
6
by: patrick t music-images dt nl | last post by:
Hi, I'm evaluating Visual Studio and the .NET concept for his company, which is currently using Borland C++ Builder. Now I tried to create components/controls etc. in .NET and I can reuse it...
3
by: danra | last post by:
Hi, I have a question which seems to me pretty basic, unfortunately I can't seem to figure it out. Let's say I have an abstract base class Vehicle and classes Car and Truck which derive from...
4
by: ma740988 | last post by:
Consider the sample source: # include <iostream> # include <string> # include <vector> using namespace std; typedef void (*VOIDFUNCPTR)(); class transfer {
253
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
4
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
16
by: sailor.gu | last post by:
Hi all guys, As an embeded programmer with five year C experience, I did read many great books related with design, coding, test,debug,algorithms, compiler, design, os, pm and others. I...
0
by: Scott Abel | last post by:
Wednesday, October 18 at 1pm EST/10am PT, Ann Rockley, CEO of The Rockley Group ,will join a panel of leading documentation experts, including Jeannette Eichholz of GE Healthcare, for a free...
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.