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 3 2017
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
On Tue, 01 Nov 2005 00:24:37 GMT, "Giovanni Bajo" <no***@sorry.co m> 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','n ow','then?')
"('what', 'now', 'then?')" b.foo('eh?')
"('eh?',)" B.foo()
'()'
Vary to taste ;-)
Regards,
Bengt Richter
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 composition mainly? What're
others?
Thanks in advance for your comments!
|
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 very easily by adding the control to the toolbox. This is great and
more 'safe' to do than in Borland.
But... although I'm nice, I'm not so nice that I am willing to give away
|
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 it.
Let's say I have abstract base class A which contains a list of
vehicles, and I define a method to insert and handle a vehicle into
|
by: ma740988 |
last post by:
Consider the sample source:
# include <iostream>
# include <string>
# include <vector>
using namespace std;
typedef void (*VOIDFUNCPTR)();
class transfer {
|
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 comments on
past experience, research articles, comments on the matter would be
much appreciated. I suspect something like C would be the best based
on comments I received from the VB news group.
Thanks for the help in advance
James Cameron
| |
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
|
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
|
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 always dream to achieve a refined and reusable design and
implementation.
I am very interested with code reuse.
|
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 one-hour
webinar, Why Enterprises MUST Adopt Content Reuse.
The panel will discuss their recent achievements in the content reuse
arena and explore how GE Healthcare is reducing documentation costs and
speeding time-to-market by marrying a solid...
|
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 in the
Sky,' Vernor Vinge described a future where software is created by
'programmer archaeologists' who search archives for existing pieces of
code, contextualize them, and combine them into new applications. So
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |