2

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Sayakiss
  • 6,878
  • 8
  • 61
  • 107

2 Answers2

6

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:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2
isinstance(a, int)

Example:

In [4]: a = 5

In [5]: isinstance(a, int)
Out[5]: True
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241