I have two classes. Engine is a parent class and Car is a child class. I defined property and setter for the property in Engine class. I want to get value of a property or change it from child class (Car).
Here is the example I work with:
#!/usr/bin/env python3
import sys
class Engine:
def __init__(self):
self.__cylinder = 4
@property
def cylinder(self):
return self.__cylinder
@cylinder.setter
def cylinder(self,cylinder):
self.__cylinder = cylinder
def start(self):
print("Engine started.")
def stop(self):
print("Engine stopped.")
class Car(Engine):
def __init__(self):
self.__wheel = 4
print("How many cylinders? - " + str(Engine.cylinder))
@property
def wheel(self):
return self.__wheel
@wheel.setter
def wheel(self, wheel):
self.__wheel = wheel
def drive(self):
self.start()
print("Car is driving")
def stop(self):
print("Car stopped")
class Tandem(Car):
pass
def main():
car = Car()
#print("How many cylinders? - " + str(car.cylinder))
if __name__ == "__main__":
main()
sys.exit(0)
The print in Car class (init function) gives this:
How many cylinders? - <property object at 0x10b754a48>
And if I uncomment print in main function it gives me error:
Traceback (most recent call last):
File "./test62.py", line 49, in <module>
main()
File "./test62.py", line 46, in main
print("How many cylinders? - " + str(car.cylinder))
File "./test62.py", line 11, in cylinder
return self.__cylinder
AttributeError: 'Car' object has no attribute '_Engine__cylinder'
What do I do wrong in each of these prints?