在f1中返回self会得到<__main__.Test instance at 0x11ae48d40>
。我想要返回'苹果和肉桂',但我不能使用str(self)。有没有办法让我做到这一点?
class Test:
def __init__(self, thing):
self.thing = thing
def f1(self, thing):
return self + " and " + thing #<<<
a = Test("apples")
a.f1("cinnamon")
apples
不是self
的字符串表示。您是否想返回self.thing
? - Martijn Pieters