4

I want to check whether the type of a variable is a specific kind in Python. For example- I want to check if var x is an int or not.

>>x=10
>>type(x)
<type 'int'>

But how can I compare their types. I tried this, but it doesn't seem to work.

if type(10)== "<type 'int'>":
    print 'yes'

How can I do this ?

1
  • 5
    It is worth noting that explicitly checking -- and branching on -- variable types is widely considered un-Pythonic since it's a direct violation of duck typing: en.wikipedia.org/wiki/Duck_typing Commented Mar 3, 2013 at 15:19

3 Answers 3

8

Use the isinstance() function to test for a specific type:

isinstance(x, int)

isinstance() takes either a single type, or a tuple of types to test against:

isinstance(x, (float, complex, int))

would test for a series of different numeric types for example.

Sign up to request clarification or add additional context in comments.

2 Comments

Worth to note, you can also use a tuple instead of a type and then it will return True if the variable is of ANY of the types given in the tuple, for example: isinstance(x, (int, float, decimal)) would return True if x is an int, or a float, or a decimal.
@InbarRose: Indeed; I always use complex as an example for that one. :-)
4

Your example could be written as:

if type(10) is int: # "==" instead of "is" would also work.
    print 'yes'

But note that it might not exactly do what you want, for example, if you wrote 10L or a number greater than sys.maxint instead of just 10, this would not print yes, because long (which would be the type of such a number) is not int.

Another way is, as Martijn already suggested, to use the isinstance() builtin function as follows:

if isinstance(type(10), int):
    print 'yes'

insinstance(instance, Type) returns True not only if type(instance) is Type but also if instance's type is derived from Type. So, since bool is a subclass of int this would also work for True and False.

But generally it is better not to check for specific types, but for for the features, you need. That is, if your code can't handle the type, it will automatically throw an exception when trying to perform an unsupported operation on the type.

If you, however, need to handle e.g. integers and floating-point numbers differently, you might want to check for isinstance(var, numbers.Integral) (needs import numbers) which evalutes to True if var is of type int, long, bool or any user-defined type which is derived from this class. See the Python documenation on the standard type hierarchy and [numbers module]

Comments

0

You can use the following ways:

>>> isinstance('ss', str)
True
>>> type('ss')
<class 'str'>
>>> type('ss') == str
True
>>> 

int - > Integer

float -> Floating Point Value

list -> List

tuple -> Tuple

dict -> Dictionary

For classes it is a little different: Old type classes:

>>> # We want to check if cls is a class
>>> class A:
        pass
>>> type(A)
<type 'classobj'>
>>> type(A) == type(cls)  # This should tell us

New type classes:

>>> # We want to check if cls is a class
>>> class B(object):
        pass
>>> type(B)
<type 'type'>
>>> type(cls) == type(B)  # This should tell us


>>> #OR
>>> type(cls) == type # This should tell us

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.