How to Determine a Python Variable's Type

Python how to check the type of a variable

Proper way to do this is isinstance

if isinstance(variable, MyClass)

But think twice if you actually need this. Python uses duck-typing, so explicit checks for types is not always a good idea. If you still want to do so, consider using some abstract base or minimal valuable type for your checks.

As other people suggesting, just getting type of your variable can be done by type(variable), but in most cases its better to use isinstance, because this will make your code polymorphic - you'll automatically support instances of subclasses of target type.

how to declare variable type, C style in python

Edit: Python 3.5 introduced type hints which introduced a way to specify the type of a variable. This answer was written before this feature became available.

There is no way to declare variables in Python, since neither "declaration" nor "variables" in the C sense exist. This will bind the three names to the same object:

x = y = z = 0

Positively Identifying Python Variable Type

you need to address python data types correctly.

'dict' is not a data type but a string, however, dict is. It is a python data type and it belongs to <class 'dict'>.


w = {}
z = []
a = type(w)
b = type(z)
if a == dict:
print ("Ok - its a dict")
else:
print('it is not')

if type(z) == list:
print('z is a list')
else:
print('z is not a list')

output:

Ok - its a dict
z is a list

Determine the type of an object?

There are two built-in functions that help you identify the type of an object. You can use type() if you need the exact type of an object, and isinstance() to check an object’s type against something. Usually, you want to use isinstance() most of the times since it is very robust and also supports type inheritance.


To get the actual type of an object, you use the built-in type() function. Passing an object as the only parameter will return the type object of that object:

>>> type([]) is list
True
>>> type({}) is dict
True
>>> type('') is str
True
>>> type(0) is int
True

This of course also works for custom types:

>>> class Test1 (object):
pass
>>> class Test2 (Test1):
pass
>>> a = Test1()
>>> b = Test2()
>>> type(a) is Test1
True
>>> type(b) is Test2
True

Note that type() will only return the immediate type of the object, but won’t be able to tell you about type inheritance.

>>> type(b) is Test1
False

To cover that, you should use the isinstance function. This of course also works for built-in types:

>>> isinstance(b, Test1)
True
>>> isinstance(b, Test2)
True
>>> isinstance(a, Test1)
True
>>> isinstance(a, Test2)
False
>>> isinstance([], list)
True
>>> isinstance({}, dict)
True

isinstance() is usually the preferred way to ensure the type of an object because it will also accept derived types. So unless you actually need the type object (for whatever reason), using isinstance() is preferred over type().

The second parameter of isinstance() also accepts a tuple of types, so it’s possible to check for multiple types at once. isinstance will then return true, if the object is of any of those types:

>>> isinstance([], (tuple, list, set))
True

What's the canonical way to check for type in Python?

Use isinstance to check if o is an instance of str or any subclass of str:

if isinstance(o, str):

To check if the type of o is exactly str, excluding subclasses of str:

if type(o) is str:

Another alternative to the above:

if issubclass(type(o), str):

See Built-in Functions in the Python Library Reference for relevant information.



Checking for strings in Python 2

For Python 2, this is a better way to check if o is a string:

if isinstance(o, basestring):

because this will also catch Unicode strings. unicode is not a subclass of str; whereas, both str and unicode are subclasses of basestring. In Python 3, basestring no longer exists since there's a strict separation of strings (str) and binary data (bytes).

Alternatively, isinstance accepts a tuple of classes. This will return True if o is an instance of any subclass of any of (str, unicode):

if isinstance(o, (str, unicode)):


Related Topics



Leave a reply



Submit