Код: Выделить всё
class Legs:
def __init__(self, amount):
self.amount = amount
def legsInfo(self):
return f"{self.amount} legs"
class Eyes:
def __init__(self, amount):
self.amount = amount
def eyesInfo(self):
return f"{self.amount} eyes"
class Animal(Legs, Eyes):
def __init__(self, name, legs_amount, eyes_amount):
self.name = name
Legs.__init__(self, legs_amount)
Eyes.__init__(self, eyes_amount)
def legsInfo(self):
return super().legsInfo()
def eyesInfo(self):
return super().eyesInfo()
# Objects
cat = Animal("Tom", 4, 2)
spider = Animal("Webster", 8, 6)
# Test de output
print(cat.legsInfo()) # 2 legs ERROR
print(cat.eyesInfo()) # 2 eyes
print(spider.legsInfo()) # 6 legs ERROR
print(spider.eyesInfo()) # 6 eyes
Это ошибка Python или какая-то «особенность»?Я изменил имя суммы на LegAmount и EyeAmount, и тогда все работает нормально. Я хочу определить/указать/установить переменную суммы как «только для этого класса» или что-то в этом роде.
Подробнее здесь: https://stackoverflow.com/questions/792 ... properties
Мобильная версия