原代码:
def intro(self):
intros = filter(lambda x: True if x else False,
[self.author, self.publisher, self.price])
return ' / '.join(intros)
错误原因:期待字符串
修改后代码:
def intro(self):
intros = filter(lambda x: True if x else False,
[self.author, self.publisher, self.price])
result = [str(item) for item in intros]
return ' / '.join(result)
1、filter()过滤出符合要求的数据列表
2、用列表推导式将数据列表转为字符串列表
3、拼接