Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

str()和直接调用__str__用法有不同吗?

遇到很奇葩的问题,请看贴图:

clipboard.png

为什么str(cut)会报错? 但cut.__str__()不会?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你该说明这是 Python 2

str 其实是个类。既然它是个类,那么它的结果自然就应该返回 str 类型的实例。于是它把会尝试把你的 cut.__str__() 的结果(unicode 类型)转成 str。在这一步报错了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...