Python 类方法不';我什么也不退
我在调用(访问?)类中的方法时遇到问题Python 类方法不';我什么也不退,python,python-3.2,Python,Python 3.2,我在调用(访问?)类中的方法时遇到问题 class dag(object): def __init__(self,temp): self.name = temp[3] self.l_o_t = temp def __str__(self): print ("The hottest temperature was:",self.l_o_t[0]) print ("The coolest temperature w
class dag(object):
def __init__(self,temp):
self.name = temp[3]
self.l_o_t = temp
def __str__(self):
print ("The hottest temperature was:",self.l_o_t[0])
print ("The coolest temperature was:",self.l_o_t[1])
print ("The average temperature was:",self.l_o_t[2])
def returnmax(self):
return self.l_o_t[0]
def returnmin(self):
return self.l_o_t[1]
def returnavg(self):
return self.l_o_t[2]
def main():
temp = dag(list_of_temperatures)
temp.returnmax()
temp.returnmin()
temp.returnavg()
temp.__str__()
当尝试打印返回值时,
returnmax
,returnmin
,returnavg
返回主程序不打印任何内容。只有print语句(如str方法中的语句)才起作用,这是为什么?Python交互式解释器为您回显所有内容,因为它是一个交互式调试器,但在Python程序中,您需要显式打印值
添加print()
调用以显示返回值:
temp = dag(list_of_temperatures)
print(temp.returnmax())
print(temp.returnmin())
print(temp.returnavg())
通常,\uuu str\uu
方法将返回字符串值,而不是在方法中使用print()
:
def __str__(self):
value = (
'The hottest temperature was: {0.l_o_t[0]}\n'
'The coolest temperature was: {0.l_o_t[1]}\n'
'The average temperature was: {0.l_o_t[2]}\n'
).format(self)
return value
然后您将使用print()
,它将调用值上的str()
,然后调用\uuu str()
方法:
print(temp)
Python交互式解释器为您回显所有内容,因为它是一个交互式调试器,但在Python程序中,您需要显式打印值 添加
print()
调用以显示返回值:
temp = dag(list_of_temperatures)
print(temp.returnmax())
print(temp.returnmin())
print(temp.returnavg())
通常,\uuu str\uu
方法将返回字符串值,而不是在方法中使用print()
:
def __str__(self):
value = (
'The hottest temperature was: {0.l_o_t[0]}\n'
'The coolest temperature was: {0.l_o_t[1]}\n'
'The average temperature was: {0.l_o_t[2]}\n'
).format(self)
return value
然后您将使用print()
,它将调用值上的str()
,然后调用\uuu str()
方法:
print(temp)
str(obj)
将调用def\uu str\uu(self)
函数,因此str函数需要返回一个值,而不是打印一个值
当函数返回一个值但您忘记打印时,您将看不到它
它与shell不同
class dag(object):
def __init__(self,temp):
self.name = temp[3]
self.l_o_t = temp
def __str__(self):
a = "The hottest temperature was:%s"%self.l_o_t[0]
b = "The coolest temperature was:%s"%self.l_o_t[1]
c = "The average temperature was:%s"%self.l_o_t[2]
return '\n'.join([a,b,c])
def returnmax(self):
return self.l_o_t[0]
def returnmin(self):
return self.l_o_t[1]
def returnavg(self):
return self.l_o_t[2]
def main():
temp = dag([27,19,23,'DAG'])
print(temp.returnmax())
print(temp.returnmin())
print(temp.returnavg())
print(temp) # print will call str, that is __str__
if __name__ == '__main__':
main()
str(obj)
将调用def\uu str\uu(self)
函数,因此str函数需要返回一个值,而不是打印一个值
当函数返回一个值但您忘记打印时,您将看不到它
它与shell不同
class dag(object):
def __init__(self,temp):
self.name = temp[3]
self.l_o_t = temp
def __str__(self):
a = "The hottest temperature was:%s"%self.l_o_t[0]
b = "The coolest temperature was:%s"%self.l_o_t[1]
c = "The average temperature was:%s"%self.l_o_t[2]
return '\n'.join([a,b,c])
def returnmax(self):
return self.l_o_t[0]
def returnmin(self):
return self.l_o_t[1]
def returnavg(self):
return self.l_o_t[2]
def main():
temp = dag([27,19,23,'DAG'])
print(temp.returnmax())
print(temp.returnmin())
print(temp.returnavg())
print(temp) # print will call str, that is __str__
if __name__ == '__main__':
main()
对于常规函数来说是不一样的,对吗?在这里,您不必使用print来调用函数值?@user3221453:无论您想在哪里向终端生成输出,都需要显式使用
print()
。谢谢。关于您对我的str方法所做的修改,.format(self)生成以下错误:indexer错误:tuple index out ofrange@user3221453:啊,我应该在那里包括一个索引。更正。这和常规函数不一样,对吗?在这里,您不必使用print来调用函数值?@user3221453:无论您想在哪里向终端生成输出,都需要显式使用print()
。谢谢。关于您对我的str方法所做的修改,.format(self)生成以下错误:indexer错误:tuple index out ofrange@user3221453:啊,我应该在那里包括一个索引。更正。如果你解释一下你做了什么,你的答案会更好。读者很难一行一行地看你做了什么不同。谢谢你的建议,布莱恩·奥克利!如果你解释一下你做了什么,你的答案会更好。读者很难一行一行地看你做了什么不同。谢谢你的建议,布莱恩·奥克利!