|
| 1 | +import copy |
| 2 | +from datetime import date |
| 3 | + |
| 4 | + |
| 5 | +def in_place(): |
| 6 | + class Person: |
| 7 | + def __init__(self, name, age): |
| 8 | + self.name = name |
| 9 | + self.age = age |
| 10 | + |
| 11 | + def __replace__(self, **changes): |
| 12 | + if unknown := changes.keys() - self.__dict__.keys(): |
| 13 | + raise AttributeError(", ".join(unknown)) |
| 14 | + self.__dict__.update(**changes) |
| 15 | + |
| 16 | + person = Person("John Doe", 42) |
| 17 | + copy.replace(person, age=24, name="Alice Smith") |
| 18 | + print(vars(person)) |
| 19 | + # This raises an error: |
| 20 | + # print(copy.replace(person, name="Bob Brown", email="[email protected]")) |
| 21 | + |
| 22 | + |
| 23 | +def shallow(): |
| 24 | + class Person: |
| 25 | + def __init__(self, name, age): |
| 26 | + self.name = name |
| 27 | + self.age = age |
| 28 | + |
| 29 | + def __replace__(self, **changes): |
| 30 | + return type(self)(**self.__dict__ | changes) |
| 31 | + |
| 32 | + person = Person("John Doe", 42) |
| 33 | + person_copy = copy.replace(person, age=24, name="Alice Smith") |
| 34 | + print(vars(person)) |
| 35 | + print(vars(person_copy)) |
| 36 | + # This raises an error: |
| 37 | + # print(copy.replace(person, email="[email protected]")) |
| 38 | + |
| 39 | + |
| 40 | +def slots(): |
| 41 | + class Person: |
| 42 | + __slots__ = ("name", "age") |
| 43 | + |
| 44 | + def __init__(self, name, age): |
| 45 | + self.name = name |
| 46 | + self.age = age |
| 47 | + |
| 48 | + def __replace__(self, **changes): |
| 49 | + instance = type(self)(self.name, self.age) |
| 50 | + for name, value in changes.items(): |
| 51 | + if hasattr(self, name): |
| 52 | + setattr(instance, name, value) |
| 53 | + return instance |
| 54 | + |
| 55 | + person = Person("John Doe", 42) |
| 56 | + person_copy = copy.replace(person, age=24, name="Alice Smith") |
| 57 | + |
| 58 | + def vars_slots(obj): |
| 59 | + return {name: getattr(obj, name) for name in obj.__slots__} |
| 60 | + |
| 61 | + print(vars_slots(person)) |
| 62 | + print(vars_slots(person_copy)) |
| 63 | + |
| 64 | + |
| 65 | +def derived(): |
| 66 | + class Person: |
| 67 | + def __init__(self, name, date_of_birth): |
| 68 | + self.name = name |
| 69 | + self.date_of_birth = date_of_birth |
| 70 | + |
| 71 | + @property |
| 72 | + def age(self): |
| 73 | + return (date.today() - self.date_of_birth).days // 365 |
| 74 | + |
| 75 | + def __replace__(self, **changes): |
| 76 | + age = changes.pop("age", None) |
| 77 | + dob = changes.pop("date_of_birth", None) |
| 78 | + |
| 79 | + instance = copy.copy(self) |
| 80 | + for name, value in changes.items(): |
| 81 | + if hasattr(self, name): |
| 82 | + setattr(instance, name, value) |
| 83 | + |
| 84 | + if age and dob: |
| 85 | + raise AttributeError( |
| 86 | + "can't set both 'age' and 'date_of_birth'" |
| 87 | + ) |
| 88 | + elif age: |
| 89 | + dob = copy.replace(date.today(), year=date.today().year - age) |
| 90 | + instance.date_of_birth = dob |
| 91 | + elif dob: |
| 92 | + instance.date_of_birth = dob |
| 93 | + |
| 94 | + return instance |
| 95 | + |
| 96 | + person = Person("John Doe", date(1983, 3, 14)) |
| 97 | + print(vars(copy.replace(person, age=24))) |
| 98 | + print(vars(copy.replace(person, date_of_birth=date(1999, 6, 15)))) |
| 99 | + # This raises an error: |
| 100 | + # print(vars(copy.replace(person, date_of_birth=date(1999, 6, 15), age=12))) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + in_place() |
| 105 | + shallow() |
| 106 | + slots() |
| 107 | + derived() |
0 commit comments