I would like to inherit all the __init__ of an instance of a class. For example if a have a class Lens and a lens "L50mm" instance of it. I would like that my camera with the "L50mm" inherit all the __init__ of the lens like this:
class Lens:
def __init__(self,focallength,aperture):
self.focallength=focallength
self.aperture=aperture
L50mm=Lens(50,2.2)
class Camera:
def __init__(self,name,sensor,lens='None'):
self.name=name
self.sensor=sensor
if lens!='None':#if I have a lens mounted I want that all the __init__ is inherited e.g.:
super(Lens).__init__()#something like this I KNOW it doesn't work
else:#if I don't have a lens just keep 'None'
self.lens=lens
mycamera=Camera('DSCF828','CCD',L50mm)
>>TypeError: must be type, not classobj
Of course I know that I can put self.lens=lens in the Camera __init__ method but I was wondering if there is a more direct way to inherit all the properties.
The main goal is to access directly all the methods:
mycamera.focallength
mycamera.aperture
instead of:
mycamera.lens.aperture
mycamera.lens.focallength
Instead self.lens = Lens() as suggested in the comment doesn't make any sense for me beacause I would have to pass all the parameters again, so please read carefully the question.
superthe right way.superis used to call a method of a superclass. Maybe here you just want to create a new instance of a lens if it isNone? In that case you just want to replace thesuper(Lens).__init__()byself.lens = Lens()