gpt4 book ai didi

python - Python中的所有内容都可以转换为字符串吗?

转载 作者:行者123 更新时间:2023-12-05 00:56:19 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com