Python 错误 TypeError: __str__ Returned Non-String but Printing Output
本文旨在解决当我们尝试打印字符串而不是在函数中使用 return 语句时出现的问题。
Python 错误TypeError: __str__ Returned Non-String but Printing Output
以下代码显示了 TypeError: str returned non-string,但它仍然打印输出。
示例代码:
class xy:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
print('X={0}, Y={1}')
if __name__ == "__main__":
x_y = xy('value of x is 1','value of y is 2')
print(x_y)
输出:
TypeError Traceback (most recent call last)
<ipython-input-1-7b40a083df6c> in <module>
10 if __name__ == "__main__":
11 x_y = xy('value of x is 1','value of y is 2')
---> 12 print(x_y)
TypeError: __str__ returned non-string (type NoneType)
为什么我们会收到错误,即 __str__
返回非字符串(类型 NoneType)? 首先,我们需要了解 __str__
运算符的工作原理。
__str__
运算符主要用于返回字符串,但我们主要使用它来打印字符串。 Python 中的 __str__
有两个术语,可互换使用,运算符或 Dunder 方法。
上述代码中,__str__方法直接调用了print()函数,打印了字符串,但没有返回字符串; 这就是它显示上述错误的原因。
此外,我们的 __str__
运算符打印字符串并且不返回任何内容; 这就是为什么它错误地使用 None 的原因。
解决 TypeError: __str__
Returned Non-String
我们可以使用 __str__
方法中的 return 语句来解决这个问题。 所以,我们必须在 __str__
方法中返回字符串而不是打印它。 请参见以下代码。
示例代码:
class xy:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return ('X={0}, Y={1}').format(self.x, self.y)
if __name__ == "__main__":
x_y = xy('value of x is 1','value of y is 2')
print(x_y)
输出:
X=value of x is 1, Y=value of y is 2
上面的代码还有一个问题。 问题是,如果我们返回的值不是字符串,它会显示确切的错误,但这次,变量类型不是 None。
示例代码:
class xy:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return 123
if __name__ == "__main__":
x_y = xy('value of x is 1','value of y is 2')
print(x_y)
输出:
TypeError Traceback (most recent call last)
<ipython-input-9-173c41d63dab> in <module>
12 if __name__ == "__main__":
13 x_y = xy('value of x is 1','value of y is 2')
---> 14 print(x_y)
TypeError: __str__ returned non-string (type int)
为了解决这个问题,我们需要对值进行类型转换并用 str()
方法包装它,这意味着将变量的类型转换为字符串,因为这是 str()
方法的要求。
示例代码:
class xy:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return str(123)
if __name__ == "__main__":
x_y = xy('value of x is 1','value of y is 2')
print(x_y)
输出:
123
现在,代码针对所有可能的情况运行,因为我们解决了所有预期的问题并满足 str 方法的要求,即它只返回字符串值。
__str__
使用 __repr__()
返回非字符串
__repr__()
方法与 str 方法做同样的事情。 请参见以下代码。
示例代码:
class calculate(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
print ("(%r, %r, %r)" %(self.x, self.y, self.z))
equation = calculate(1, 2, 3)
print(equation)
输出:
(1, 2, 3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-0ee74d4c74c8> in <module>
8
9 equation = calculate(1, 2, 3)
---> 10 print(equation)
TypeError: __str__ returned non-string (type NoneType)
上面的代码还打印了字符串,而不是从 __repr__()
方法返回它。 因此,我们可以使用 return 语句来解决它,而不是使用 __repr__()
方法打印字符串。
示例代码:
class calculate(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return str("(%r, %r, %r)" %(self.x, self.y, self.z))
equation = calculate(1, 2, 3)
print(equation)
输出:
(1, 2, 3)
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串