Я пытаюсь узнать, как классы работают на Python, и новичок в этом, у меня есть следующий класс, определенный как 'Animal':
class Animal(object):
def __init__(self, size, color, mood):
# init: consists of statements that bind the parameters passed to init to the instance o f the class, when an instance
# is created.
self.size = size
self.color = color
self.mood = mood
self.alive = True
def feeling(self):
# first method, methods are limited to the class, that is why their name does not need to be unique.
return "The", self.color, str(self), " is feeling", self.mood, "."
def colors(self, other):
return "The", str(self), "is", self.color, "and the", str(other), "is", other.color, "."
Затем я создаю экземпляр объекта Animal как follow:
hippo = Animal("large", "purple", 'amused')
Наконец, я вызываю метод для моего объекта следующим образом:
print(hippo.feeling())
Я ожидаю получить вывод, как показано ниже:
"The purple hippo is feeling amused."
Но что я получу в выводе, если напечатаю тот же аргумент, что и выше:
('The', 'purple', '<__main__.Animal object at 0x7f43cc978160>', ' is feeling', 'amused', '.')
Может кто-нибудь объяснить, пожалуйста, почему вывод похож на список? также почему str (self) вернул имя объекта, а не слово hippo.
Оригинальный код в учебнике был написан на Python 3.5, я подумал, что это могло быть причиной, но я попробовал онлайновую IDE https://www.jdoodle.com/python3-programming-online/ для Python 3.5.1 и результат был таким же.