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?
7 Respuestas
+ 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?
+ 1
You must call super ctor at start of __init__ ... That its
+ 1
👍👍👍
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
0
what do I get from that
class Sub1(Base):
def __init__ (self):
super().__init__ (1,2,3,4)
???
how to use super().__init__???
0
thanks dude