I'm not sure what I'm doing wrong here:
>>> class Stringy(object):
... def __str__(self):
... return "taco"
... def __repr__(self):
... return "taco"
...
>>> lunch = Stringy()
>>> lunch
taco
>>> str(lunch)
'taco'
>>> '-'.join(('carnitas',lunch))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected string, Stringy found
Given my inclusion of the __str__()
method in the Stringy object, shouldn't join()
see lunch as a string?
no you have to convert it to str yourself
'-'.join(('carnitas',str(lunch)))
if you have to do it for a whole sequence of items
'-'.join(str(x) for x in seq)
or
'-'.join(map(str, seq))
for your particular case you can just write
'carnitas-'+str(lunch)
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