Why this simple code doesn't work for Python 2.7 ? Please, help. Most likely I misuse super
method in 'New Style' for classes.
class Mechanism(object):
def __init__(self):
print('Init Mechanism')
self.__mechanism = 'this is mechanism'
def get_mechanism(self):
return self.__mechanism
class Vehicle(object):
def __init__(self):
print('Init Vehicle')
self.__vehicle = 'this is vehicle'
def get_vehicle(self):
return self.__vehicle
class Car(Mechanism, Vehicle):
def __init__(self):
super(Car, self).__init__()
c = Car()
print(c.get_mechanism())
print(c.get_vehicle())
The error:
Init Vehicle
Traceback (most recent call last):
File "check_inheritance.py", line 22, in <module>
print(c.get_mechanism())
File "check_inheritance.py", line 7, in get_mechanism
return self.__mechanism
AttributeError: 'Car' object has no attribute '_Mechanism__mechanism'
EDIT
- Fixed
def __init(self):
inMechanism
class ontodef __init__(self):
- The correct answer is to use
super
method in all classes. Not only inCar
class. See the answer of Martijn Pieters - Try to avoid double underscore
__
for private variables. It is not a Python way (style of code). See the discussion for more info here.