# Полиморфизм # Проблема from functools import total_ordering class Rectangle: def __init__(self, a, b): self.a = a self.b = b def get_area(self): return self.a * self.b class Square: def __init__(self, a) -> None: self.a = a def get_area(self): return self.a**2 class Circle: def __init__(self, r): self.r = r def get_area(self): return 3.14*self.r**2 class UnitedKingdom: def __init__(self) -> None: pass @staticmethod def get_capital(): print("London is the capital of Great Britain.") @staticmethod def get_language(): print("English is the primary language of Great Britain.") class Spain: def __init__(self): pass @staticmethod def get_capital(): print("Madrid is the capital of Spain.") @staticmethod def get_language(): print("Spanish is the primary language of Spain.") class DateUSA: def __init__(self, day, month, year) -> None: self.day = day self.month = month self.year = year def format(self): return f"{self.month:02}/{self.day:02}/{self.year:04}" def isoformat(self): return f"{self.year:04}-{self.month:02}-{self.day:02}" class DateEurope: def __init__(self, day, month, year): self.day = day self.month = month self.year = year def format(self): return f"{self.day:02}/{self.month:02}/{self.year:02}" def isoformat(self): return f"{self.year:04}-{self.month:02}-{self.day:02}" @total_ordering class BankAccount: def __init__(self, name, balance): self.name = name self.balance = balance def __str__(self): return self.name def __len__(self): return len(str(self)) def __lt__(self, other): if isinstance(other, BankAccount): return self.balance < other.balance elif isinstance(other, int): return self.balance < other else: raise ValueError def __add__(self, other): if isinstance(other, BankAccount): return self.balance + other.balance elif isinstance(other, Numbers): return self.balance + sum(other._values) elif isinstance(other, (int, float)): return self.balance + other else: raise ValueError def __radd__(self, other): return self.__add__(other) class Numbers: def __init__(self, values: list): self._values = values def __add__(self, other): if isinstance(other, Numbers): return sum(other._values) + sum(other._values) elif isinstance(other, BankAccount): return sum(self._values) + other.balance elif isinstance(other, (int, float)): return sum(self._values) + other else: raise ValueError def __radd__(self, other): return self.__add__(other) def main(): lst = [4, BankAccount('Petr', 100), 5] print(sum(lst)) # usa = DateUSA(1, 3, 2016) # print(usa.format()) ''' rect1 = Rectangle(3, 4) rect2 = Rectangle(12, 5) sq1 = Square(4) sq2 = Square(5) figures = [rect1, rect2, sq1, sq2] for figure in figures: print(figure.get_area()) ''' if __name__ == '__main__': main()