Python type() 函数获取类型字符串 - 获取对象类型名称
在 Python 中,可以使用 type() 函数来获取一个对象的类型。type() 函数返回的是一个 type 对象,可以通过调用 type() 函数的返回值的 __name__ 属性来获取类型的字符串表示。
以下是一个示例:
x = 5
print(type(x).__name__) # 输出: int
y = "hello"
print(type(y).__name__) # 输出: str
z = True
print(type(z).__name__) # 输出: bool
注意,type() 函数返回的是一个 type 对象,而不是字符串。要获取类型的字符串表示,需要调用 type() 函数返回值的 __name__ 属性。
原文地址: https://www.cveoy.top/t/topic/p9fw 著作权归作者所有。请勿转载和采集!