+ 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(): ...

30th May 2018, 4:26 AM
Jan Markus
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
30th May 2018, 6:41 AM
Dan Walker
Dan Walker - avatar
+ 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 :)
3rd Jun 2018, 8:50 PM
warlord
warlord - avatar
+ 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.
3rd Jun 2018, 5:39 PM
Maninder $ingh
Maninder $ingh - avatar
+ 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
30th May 2018, 6:14 AM
Pedro Demingos
Pedro Demingos - avatar
+ 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.
30th May 2018, 7:27 AM
Pulkit Kamboj
Pulkit Kamboj - avatar
+ 12
@ Scorpia Rising but what, if it is a builtin Python-class, e.g. the list-class?
30th May 2018, 4:33 AM
Jan Markus
+ 9
Frog You can have both object orientation and functional programming at the same time. Python may not be the best but you can indeed write the majority of an application with objects
3rd Jun 2018, 3:40 PM
Dan Walker
Dan Walker - avatar
+ 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)
3rd Jun 2018, 4:25 PM
Deji Femi
Deji Femi - avatar
+ 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!
3rd Jun 2018, 9:18 PM
Andriy Kushynskyy
Andriy Kushynskyy - avatar
+ 7
Cut it and paste it to your new class😂😂😂
30th May 2018, 4:29 AM
Scorpia Rising🎩
Scorpia Rising🎩 - avatar
+ 6
Will there be any speed issue if you inherit entire class A?
3rd Jun 2018, 3:37 PM
Prathamesh Sawant
Prathamesh Sawant - avatar
+ 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
4th Jun 2018, 2:23 AM
Prashanth Kumar
Prashanth Kumar - avatar
+ 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.
30th May 2018, 5:51 PM
Pedro Demingos
Pedro Demingos - avatar
+ 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)
3rd Jun 2018, 3:26 PM
ivankoshan
ivankoshan - avatar
+ 5
bookmarking
3rd Jun 2018, 5:25 PM
Masquerade
Masquerade - avatar
+ 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)
4th Jun 2018, 2:24 PM
Oma Falk
Oma Falk - avatar
+ 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).
3rd Jun 2018, 4:01 PM
Frog
Frog - avatar
+ 4
there is no such metod in python. try ctrl+c & ctrl+v
3rd Jun 2018, 4:26 PM
Николай Серов
+ 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.
3rd Jun 2018, 6:41 PM
Umair Salam
Umair Salam - avatar
+ 4
I think the question needs an example code, not a lot of text.
3rd Jun 2018, 7:32 PM
Xarbi Goals
Xarbi Goals - avatar