I have a simple piece of code. In it I make 2 animals a cat and a spider. They have a difference number of legs and eyes. I just want to print these data.
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
It appears that because of the property amount is used in both classes, this value is shared. Which I think is very strange, it are different classes, so there is no need to share.
Is this a Python bug or some kind of “special feature”?
I have changed the name amount to legsAmount and eyesAmount and then it works fine. I want to define / specify / set the amount variable as “only for this class” or something.