How can I inherit a constructor with ability to add some other attributes for each subclasses's constructor? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I inherit a constructor with ability to add some other attributes for each subclasses's constructor?

I have a superclass and a couple of its subclasses. Each subclass has a lot of objects, but each subclass has the same constructor with the difference in only one object attribute. I want to define the constructor in the superclass in order not to repeat myself millions of times in each subclass, but I can't, because then I can not program the behavior of that one differing attribute. how can I inherit a constructor, but keeping that unique attribute for each subclass?

21st May 2018, 8:57 PM
hide on bush
hide on bush - avatar
7 Answers
+ 2
class Base: def __init__(a,b,c,d): ..... class Sub1(Base): def __init__(e): super().__init__(1,2,3,4) ...... class Sub2(Base): def __init__(e): super().__init__(5,6,7,8) ...... But i dont think that you want this... Can you explain better with an example?
21st May 2018, 9:46 PM
KrOW
KrOW - avatar
+ 1
You must call super ctor at start of __init__ ... That its
21st May 2018, 10:07 PM
KrOW
KrOW - avatar
+ 1
👍👍👍
22nd May 2018, 6:48 PM
KrOW
KrOW - avatar
0
class Drawable: def __init__(self,multiplicator, color) self.multiplicator = multiplicator self.color = color def draw(self): #this function draws self.shape that will be unique for each object # class Something(Drawable): def __init__(self): self.shape = #now I need to define my self.shape using also self.color and self.multiplicator
21st May 2018, 10:05 PM
hide on bush
hide on bush - avatar
0
what do I get from that class Sub1(Base): def __init__ (self): super().__init__ (1,2,3,4) ??? how to use super().__init__???
22nd May 2018, 5:11 PM
hide on bush
hide on bush - avatar
0
thanks dude
22nd May 2018, 6:39 PM
hide on bush
hide on bush - avatar