PDA

View Full Version : Parent constructor not invoked with Python



vikkyhacks
January 10th, 2013, 04:30 AM
I have the code


class A:
def __init__(self):
print('Class A loaded')

class B(A):
def __init__(self):
print('Class B loaded')


b = B()

and this produces the output,


Class B loaded

I have more of C and Java background and in them the parent constructor is automatically invoked when the parent class is created, but why not in python. Why is the parent constructor not invoked ???

ofnuts
January 10th, 2013, 09:19 AM
I have the code


class A:
def __init__(self):
print('Class A loaded')

class B(A):
def __init__(self):
print('Class B loaded')


b = B()and this produces the output,


Class B loadedI have more of C and Java background and in them the parent constructor is automatically invoked when the parent class is created, but why not in python. Why is the parent constructor not invoked ???

Not in Python: http://stackoverflow.com/questions/3782827/why-arent-pythons-superclass-init-methods-automatically-invoked

To call your parent class initialisation: http://stackoverflow.com/questions/2399307/python-invoke-super-constructor

Bachstelze
January 10th, 2013, 05:35 PM
I have more of C and Java background and in them the parent constructor is automatically invoked when the parent class is created, but why not in python. Why is the parent constructor not invoked ???

"constructor" in C, really?

vikkyhacks
January 11th, 2013, 05:36 PM
"constructor" in C c++ actually

MG&TL
January 11th, 2013, 05:38 PM
c++ actually

Then be more careful typing here. Bachstelze gets grumpy when people mix C and C++. ;)

vikkyhacks
January 12th, 2013, 02:38 PM
Thanks, and apologies for the late response, I am a bit slow learner and took time for me to understand !!!

):P