Почему первый код печатается, а второй - нет? Есть что-то особенное в возвращении?python return and print через класс
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)
Вам нужно вернуться во втором. – badc0re
@ badc0re: Ах, вот и все! Я сидел здесь, косясь, пытаясь понять, как эти два класса отличаются в первую очередь. | -} –