Joining a list containing an object - is there any magic method I could set to convert the object to a string before join fails?
', '.join([…, Obj, …])
I tried __str__
and __repr__
but neither did work
Nope, there's no join
hook (although I've wanted this feature as well). Commonly you'll see:
', '.join(str(x) for x in iterable)
or (almost) equivalently:
', '.join(map(str,iterable))
', '.join([str(x) for x in iterable])
(Note that all of the above are equivalent in terms of memory usage when using CPython as str.join
implicitly takes your generator and turns it into a tuple anyway.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With