这是从Learning Python中提取和改编的代码,lambda
第5行有什么用return lambda *args: '[Getattr str]'
,似乎print(X.__str__())
期望0个参数,*args
这里没有必要吗?
class GetAttr:
def __getattr__(self, attr):
print('getattr: ' + attr)
if attr == '__str__':
return lambda *args: '[Getattr str]'
else:
return lambda *args: None
X = GetAttr()
print(X.__call__('why', 'any', 'number', 'of', 'arguments', 'here'))
print(X.__str__()) # but not here?