Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
加入包含对象的列表 - 在加入失败之前,我可以设置任何魔术方法将对象转换为字符串吗?
', '.join([…, Obj, …])
我试过了__str__,__repr__但都没有工作
__str__
__repr__
不,没有join钩子(虽然我也想要这个功能)。通常你会看到:
join
', '.join(str(x) for x in iterable)
或(几乎)等效地:
', '.join(map(str,iterable)) ', '.join([str(x) for x in iterable])
(请注意,在使用 CPython 时,上述所有内容在内存使用方面都是等效的,因为str.join它会隐式地使用您的生成器并将其转换为元组。)
str.join