1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Baseclass1: def __init__( self ,x,y): self .x = x self .y = y print ( self .x + self .y) class Derived(Baseclass1): def cmethod( self ): Baseclass1.__init__( self ,x,y) #Base constructor called with class name print ( self .x * self .y) ob = Derived( 4 , 5 ) ob.cmethod() |
Error:Traceback (most recent call last):
File "C:/Python37/understandingConstructor.py", line 33, in <module>
ob.cmethod()
File "C:/Python37/understandingConstructor.py", line 29, in cmethod
Baseclass1.__init__(self,x,y)#Base constructor called with class name
NameError: name 'x' is not defined
here I am not able to override construcor of the base class and getting error.probably my code is wrong...but can anyone explain how constructor overriding code in python with proper example...