这是代码:
class Animal:
def __init__(self, animal_type):
self.animal_type = animal_type
class Cat(Animal):
def __init__(self, animal_type, favorite_food):
super().__init__(animal_type)
self.favorite_food = favorite_food
def cat_print(self):
print("{}'s favorite food is {}.".format(self.animal_type, self.favorite_food))
def __getattribute__(self, item):
print('__getattribute__() be called. Item is: ', item)
def __getattr__(self, item):
print('__getattr__() be called. Item is: ', item)
def __setattr__(self, key, value):
print('__setattr__() be called. key and Value is: ', key, value)
cat = Cat('cat', 'fish')
print(cat.animal_type)
print(cat.favorite_food)
当我打印时cat.animal_type
,它打印无。我猜是因为我重写了 method: __setattr__()
and __getattribute__()
,所以值不能传递给属性。
我想知道在python中分配属性并获取类中的属性的过程是什么?
谢谢。