forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.py
74 lines (55 loc) · 2.04 KB
/
car.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Car, Device, and Vehicle classes
Used to demonstrate object oriented techniques in Java vs Python
"""
class Vehicle:
"""The Vehicle class is the parent for all vehicles."""
def __init__(self, color, model):
"""Define the color and model of our vehicle"""
self.color = color
self.model = model
class Device:
"""The Device class defines objects which have a battery."""
def __init__(self):
"""Define the base voltage for our device."""
self._voltage = 12
class Car(Vehicle, Device):
"""The Car class is both a Vehicle and a Device."""
wheels = 0
def __init__(self, color, model, year):
"""Call our parent classes, then define the year."""
Vehicle.__init__(self, color, model)
Device.__init__(self)
self.year = year
def add_wheels(self, wheels):
"""Change the number of wheels we have."""
self.wheels = wheels
@property
def voltage(self):
"""Allow us to access the Device._voltage property as voltage"""
return self._voltage
@voltage.setter
def voltage(self, volts):
"""Warn the user before resetting the voltage"""
print("Warning: this can cause problems!")
self._voltage = volts
@voltage.deleter
def voltage(self):
"""Warn the user before deleting the voltage"""
print("Warning: the radio will stop working!")
del self._voltage
def __str__(self):
"""Improved human readable version of the object"""
return f"Car {self.color} : {self.model} : {self.year}"
def __eq__(self, other):
"""Do these objects have the same year?"""
return self.year == other.year
def __lt__(self, other):
"""Which object was made earlier than the other?"""
return self.year < other.year
def __add__(self, other):
"""Add the objects together in our predefined way."""
return Car(
self.color + other.color,
self.model + other.model,
int(self.year) + int(other.year),
)