如何更改内置的 type() 函数?

How to change built-in type() function?

可以更改 type() 的默认行为吗? 这是现在发生的事情:

simple_string = "this is a simple string"
type(simple_string)
<class 'str'>

这就是我希望它的工作方式或类似的方式:

simple_string = "this is a simple string"
type(simple_string)
"str"

您可以使用 __name__ 属性

 simple_string = "this is a simple string"
 type(simple_string).__name__

>>> 'str'

在python,everything is an object.

type() 函数只是 return 对 class 的引用,该对象是其实例。把 class 想象成模具,把物体想象成从模具中创造出来的东西。

type() 函数将 return 一个 python 对象引用到 class 派生对象。要获取 class 的名称(在本例中为 'str'),您可以访问 name 属性,如下所示:

my_class = type('my string')
my_class_name = class.__name__