Метод порівняння об’єктів gt
Наприклад, якщо у вас є клас
Наприклад, якщо у вас є клас
Person
з атрибутом age
, ви можете визначити метод gt(self, other)
, щоб порівнювати людей за віком.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __gt__(self, other):
return self.age > other.age
# Створюємо два об'єкти класу Person
person1 = Person("Alice", 25)
person2 = Person("Bob", 30)
# Порівнюємо їх вік
if person1 > person2:
print(f"{personl name} старший, ніж {person2.name}")
else:
print(f"{person1.name} молодший, ніж {person2.name}")
#Python // #theory // Вакансії IT