1 回答
TA贡献1831条经验 获得超10个赞
如果您确定父类始终是Guy,您可以简单地通过以下方式进行注释@Guy.greeting_decorator:
class Guy:
def __init__(self, name):
self.name = 'John'
def greeting_decorator(original_function):
def return_function(self, *args):
return f'Hi, I\'m {self.name}, fullname: {original_function(self, *args)}'
return return_function
class GuyWithSurname(Guy):
def __init__(self, name, surname):
super().__init__(name)
self.surname = surname
@Guy.greeting_decorator # <----- here
def __str__(self):
return f'{self.name} {self.surname}'
JohnDoe = GuyWithSurname('John', 'Doe')
这样,当你调用print(JohnDoe)它时就会输出Hi, I'm John, fullname: John Doe.
请注意,我必须更改greeting_decorator和return_function参数才能正确处理self.
添加回答
举报