I have a top level class Car
with Tire
class as a property. When I update Tire
from a top-level instantiation of the class (honda
in example below), it also updates the other instance (bmw
in example below).
Is there a way to make the assert statement below True? I could make Tire
inherit from NamedTuple
, but then it would be immutable, and I would not be able to simple update a property such as with honda.tire.tread_count=44
.
Example Program with failing assert:
class Tire():
def __init__(self, tread_count: int, size: int):
self.tread_count = tread_count
self.size = size
class Car():
def __init__(self, color: str = "red", tire: Tire = Tire(tread_count=50, size=20)):
self.color = color
self.tire = tire
# Instantiate honda and change a property.
honda = Car()
print(honda.tire.tread_count)
honda.tire.tread_count=44
print(honda.tire.tread_count)
# Instantiate bmw.
bmw = Car()
print(bmw.tire.tread_count)
assert bmw.tire.tread_count == 50
# Question -- How can we make bmw not get updated by updating honda?
Output of example program:
50
44
44
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
17 bmw = Car()
18 print(bmw.tire.tread_count)
---> 19 assert bmw.tire.tread_count == 50
20 # Question -- How can we make bmw not get updated by updating honda?
AssertionError: