Alex Rivera | Logout

Determine the type of an object?

Asked 2010-02-08T21:37:20.240
2230

Is there a simple way to determine if a variable is a list, dictionary, or something else?

Edit
Report

1 Answer

38

On instances of object you also have the:

__class__

attribute. Here is a sample taken from Python 3.3 console

>>> str = "str"
>>> str.__class__
<class 'str'>
>>> i = 2
>>> i.__class__
<class 'int'>
>>> class Test():
...     pass
...
>>> a = Test()
>>> a.__class__
<class '__main__.Test'>

Beware that in python 3.x and in New-Style classes (aviable optionally from Python 2.6) class and type have been merged and this can sometime lead to unexpected results. Mainly for this reason my favorite way of testing types/classes is to the isinstance built in function.

answered 2013-06-26T21:38:42.853

Your Answer