Sayalic
Sayalic

Reputation: 7530

How to know the type of a object in a simple way?

I always write code like:

str(type(a)).find('int') != -1

Or

t = str(type(a)).split("'")[1]

Is there any simple way to do that?

Upvotes: 2

Views: 85

Answers (2)

alecxe
alecxe

Reputation: 474191

Looks like you are asking about isinstance():

>>> a = 1
>>> isinstance(a, int)
True
>>> s = "test"
>>> isinstance(s, str)
True

Speaking about the the second example (string type), it is important to note that there is a basestring type:

Upvotes: 6

inspectorG4dget
inspectorG4dget

Reputation: 114035

isinstance(a, int)

Example:

In [4]: a = 5

In [5]: isinstance(a, int)
Out[5]: True

Upvotes: 2

Related Questions