Search code examples
pythonobjectobject-type

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?


Solution

  • 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: