Gustavo
Gustavo

Reputation: 41

How to change built-in type() function?

It is possible to changing the default behavior of the type()? This is what happens now:

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

This is how a I want it to work or something similar:

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

Upvotes: 2

Views: 596

Answers (2)

Daniel Long
Daniel Long

Reputation: 1647

In python, everything is an object.

The type() function simply returns a reference to the class of which the object is an instance of. Think of the class like the mold and the object as something created from that mold.

The type() function will return a python object reference to the class from which the object is derived. To get the name of the class (in this case 'str'), you can access the name property as follows:

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

Upvotes: 3

Matt&#233;o
Matt&#233;o

Reputation: 42

You can use __name__ property

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

>>> 'str'

Upvotes: 3

Related Questions