我正在尝试在 Python 中找到一个无法转换为字符串的示例。
>>> str(None)
'None'
>>> str(False)
'False'
>>> str(5)
'5'
>>> str(object)
"<class 'object'>"
>>> class Test:
... pass
...
>>> str(Test)
"<class '__main__.Test'>"
>>> str(Test())
'<__main__.Test object at 0x7f7e88a13630>'
整个 Python 世界中是否有任何东西不能转换为 str?
最佳答案
Is everything in Python castable to a string?
不!
>>> class MyObject():
... def __str__(self):
... raise NotImplementedError("You can't string me!")
...
>>> str(MyObject())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in __str__
NotImplementedError: You can't string me!
关于python - Python中的所有内容都可以转换为字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62460727/