python - Python中类型的字符串表示
问题描述
我希望在 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 字符串的正确方法是什么?
解决方案
也许你想要
type("cow").__name__
?
推荐阅读
- php - 如何从数组中获取元素?
- laravel - 组件 vuejs / laravel 之间的事件
- javascript - 如何在 jQuery 中使用 iframe 的“execCommand”方法?
- sql - SQL Server:具有相同 ID 的 SUM 列
- linux-kernel - 从不兼容的指针类型传递“proc_create”的参数 4
- android - nativescript(vue.js) 中需要 NFC 权限
- python - 无法为 ipopt、pyomo、cython 创建 AWS Lambda 部署包
- testing - Postman - 如何在发送请求之前为正文中的所有“key”=“value”对设置一个特定的“value”
- c# - 调试器如何匹配我的代码和纯汇编代码?
- sql - Vertica SQL - 拉取列中的特定值,