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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With