from datetime import date from functools import singledispatchmethod class BirthInfo: @singledispatchmethod def __init__(self, birth_date): raise ValueError(f"unsupported date format: {birth_date}") @__init__.register(date) def _from_date(self, birth_date): self.date = birth_date @__init__.register(str) def _from_isoformat(self, birth_date): self.date = date.fromisoformat(birth_date) @__init__.register(int) @__init__.register(float) def _from_timestamp(self, birth_date): self.date = date.fromtimestamp(birth_date) def age(self): return date.today().year - self.date.year class Person: def __init__(self, name, birth_date): self.name = name self._birth_info = BirthInfo(birth_date) @property def age(self): return self._birth_info.age() @property def birth_date(self): return self._birth_info.date john = Person("John Doe", date(1998, 5, 15)) print(john.age) print(john.birth_date) jane = Person("Jane Doe", "2000-11-29") print(jane.age) print(jane.birth_date) linda = Person("Linda Smith", 1011222000) print(linda.age) print(linda.birth_date)