join函数遇到的问题
来源:9-1 viewmodel意义的体现与filter函数的巧妙应用

autista
2018-04-22
class BookViewModel: def __init__(self, book): self.title = book['title'], self.publisher = book['publisher'], self.pages = book['pages'] or '', self.author = '、'.join(book['author']), self.price = book['price'], self.summary = book['summary'] or '', self.image = book['image'] @property def intro(self): intros = filter(lambda x: True if x else False, [self.author, self.publisher, self.price]) return ' / '.join(intros)
按照视频的写法,报错:
File "C:\Users\ts\PycharmProjects\fisher\app\view_models\book.py", line 42, in intro return ' / '.join(intros) TypeError: sequence item 0: expected str instance, tuple found
pycharm中调试可以看到,self.author, self.publisher, self.price格式都是tuple,所以报错,奇怪的的是,视频中确可以,也没看到老师的BookViewModel中对这3个数据做了类型处理~感觉很奇怪。
写回答
1回答
-
为什么这个问题这么多人犯?想不明白,你是第七个了,python每行代码后面需要输入逗号?
012018-04-23
相似问题