gpt4 book ai didi

python - 获取 'type' 作为人类可读字符串的返回值

转载 作者:太空狗 更新时间:2023-10-29 22:11:39 24 4
gpt4 key购买 nike

如何将 type 的可变部分作为字符串获取?

即:

>>> type('abc')
<type 'str'>
>>> type(1)
<type 'int'>
>>> type(_)
<type 'type'>

在这里的每种情况下,我都想要单引号内的内容:str、int,类型为字符串。

我尝试对 repr(type(1)) 使用正则表达式,这很有效,但这似乎并不健壮或 Pythonic。有没有更好的办法?

最佳答案

可以通过type(1).__name__获取名字

关于python - 获取 'type' 作为人类可读字符串的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13477924/

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