为什么第一个代码打印而第二个不打印?退货有什么特别之处吗?
In [339]: class fraction:
def __init__(self,top,bottom):
self.num=top
self.den=bottom
def __str__(self):
return str(self.num)+"/"+str(self.den)
.....:
In [340]: f=fraction(3,8)
In [341]: print(f)
3/8
In [342]: class fraction:
def __init__(self,top,bottom):
self.num=top
self.den=bottom
def __str__(self):
print str(self.num)+"/"+str(self.den)
.....:
In [343]: f=fraction(3,8)
In [344]: print(f)
3/8
TypeError Traceback (most recent call last)
<ipython-input-344-d478acf29e40> in <module>()
----> 1 print(f)
TypeError: __str__ returned non-string (type NoneType)