Joining a list of python objects with __str__ method

前端 未结 5 788
孤城傲影
孤城傲影 2021-01-02 09:44

I\'ve already looked at this question on representing strings in Python but my question is slightly different.

Here\'s the code:

>>> class W         


        
5条回答
  •  无人及你
    2021-01-02 10:07

    You have to stringify your objects before you can join them. This is because str.join expects a series of strings, and you must give it a series of strings.

    For the sake of less typing at the cost of readability, you can do "\n".join(map(str, list_of_things).

提交回复
热议问题