+ 69
How to inherit only a small part of methods from a large class A into a new class B without inheriting all of A in Python?
Assumed there is a class A with dozens of methods. For my purposes I need only 2 or 3 "interesting" methods of the class A in my new class B. Further on I want to extend the class B with some of my own methods. Inheritance is commonly done with class B(A): def method1(): ... def method2(): ... But then I have a bunch of superfluous methods of A in B. Is there a way that I can pick some methods from A similar as in the import-statement, where I can limit the accessed methods, so to say a partial inheritance. I would like to have something as class B(from A take methodX, methodY): def newmethod1(): ... def newmethod2(): ... def newmethod3(): ...
40 Réponses
+ 63
Inheritance represents an "is a" relationship i.e. class B is a class A (a dog is a mammal). It may be that your design is incorrect, if your inheriting class doesn't need most of the functionality of its parent. You could consider composition instead, are the methods of B really methods of the object? (e.g. Dog can eat and walk, but shouldn't 'fillBowl' belong to the owner?) If this is the case, extract these useful methods to another class 'Util', and then B "has a" Util and A can have one too. This is another way of sharing code without having bloated inheritance trees and access to methods you probably should not
+ 23
For Example In Paramiko SSH Library There Is A Server Class And You Can Make It Based Like
class Test(paramiko.ServerInterface):
And You Don't Need The __init__ To Use The Class But It Will Automatically Run The Main Commands In paramiko.ServerInterface.__init__
I Hope This Helped :)
+ 15
Here is solution of your problem.
class Car():
def__init__(self,car,colour,price):
self.car=car
self.colour=colour
self.price=price
class NewCar(Car):
def__init__(self,car,colour,price,acc):
super().__init__(colour,price)
self.car=car
self.acc=acc
Here NewCar class take only two 2 attributes from Car class but Car class has 3 attributes.
+ 13
If two classes have some methods in common, it seems natural to me that they should be subclasses of an upper class. Then you put every method in its right position.
But I don’t know the answer to your actual question :P
+ 13
There is no method to only inherit only one or two methods or functions from a given class and the reason is that we can inherit the entire class and call the needed methods or function from the class to the class in which we need it so why should we inherit only one or two methods.
+ 12
@ Scorpia Rising
but what, if it is a builtin Python-class, e.g. the list-class?
+ 9
class A:
def __init__(self, item1, item2):
self.item1=item1
self.item2=item2
class B(A):
def inherit(self):
result=list([self.item1, self.item2])
return result
C = B("1", "2")
D=C.inherit()
print (D)
+ 9
For this issue you don’t need inheritance. what you need is to incapsulate A class into B and implement only needed methods:
class B:
def __init__(self)
self._a = A()
def func1(arg):
self._a.func1(arg)
def func2(arg):
self._a.func2(arg)
in this approach, when you create the obj of B class you will have only func1() and func2() avaliable to see and use.
And that is what you want ;)
Enjoy!
+ 7
Cut it and paste it to your new class😂😂😂
+ 6
Will there be any speed issue if you inherit entire class A?
+ 6
Dan Walker
now JavaScript beats python in this topic...
😙😙😙😙
we have a super method in JavaScript which can allow us to use only a particular trait to be use in the child class
+ 5
If you’re making a class inherited from a builtin class, you can also inherit everything then override what you want. Maybe it takes a few lines, but would be organized I think.
+ 5
I dont know how to do that . i would just copy method from class A and paste in class B (im not good in python so i dont know if thats possible)
+ 5
bookmarking
+ 5
if there is a need to inherit only a few methods, it is an indicator that the design is not optimal.
Obviously Interfaces make more sense.
I would redesign the project and stop going on the wrong way(and did it several times)
+ 4
Dan Walker that's correct. the advantage from Python over the C languages though is the functional programming, a privilege from Python only (regarding to the C's. there are a few other languages which give you functional programming).
+ 4
there is no such metod in python. try ctrl+c & ctrl+v
+ 4
As far as i know, you can’t pick and choose parent’s functionality when inheriting a python class. i dont think you can do that in other object oriented languages either.
+ 4
I think the question needs an example code, not a lot of text.