Skip to main content
 首页 » 编程设计

python之Python 中类型的字符串表示

2025年05月04日33myhome

我希望在 eval 语句中使用类型函数,因此我只需要该函数的字符串表示形式。例如:

print(type("cow")) 
<class 'str'> 

这里我需要它来输出'str'。但是当我尝试时:

type("cow").__str__() 
TypeError: descriptor '__str__' of 'str' object needs an argument 
type("cow").__repr__() 
TypeError: descriptor '__repr__' of 'str' object needs an argument 

奇怪的是,如果那是单元格中的最后一行,Jupyter notebook 会正确打印它。

为什么会出现这个错误?仅获取类型字符串的正确方法是什么?

请您参考如下方法:

也许你想要

type("cow").__name__ 

?