Questions tagged [isinstance]

isinstance is a Python built-in function used to test whether or not a specific object is an instance of a particular class or type. It is available in both versions 2 and 3. Use this tag for questions explicitly dealing with the isinstance built-in.

isinstance is a Python built-in function used to test whether or not a specific object is an instance of a particular class or type. It is available in both versions 2 and 3. Use this tag for questions explicitly dealing with the isinstance built-in.

The schematics for the function are below:

isinstance(object, class-or-type-or-tuple) -> bool

where object is the object to test and class-or-type-or-tuple is a class, type, or tuple of classes and/or types.

When invoked, and given a single class or type as its second argument, the function will return either True or False depending on whether or not the first argument is an instance of the second. Below is a demonstration:

>>> isinstance(1, int)
True
>>> isinstance(1, str)
False
>>>

If the function is given a tuple of classes and/or types as its second argument, it will return either True or False depending on whether or not the first argument is an instance of any of the classes/types contained in the tuple. Below is a demonstration:

>>> isinstance('a', (int, str))
True
>>> isinstance('a', (int, bool))
False
>>>

It should also be noted that the following code:

isinstance('a', (int, str))

is equivalent to this:

isinstance('a', int) or isinstance('a', str)
193 questions
102
votes
5 answers

Python check if isinstance any type in list?

How do I pythonicly do: var = 7.0 var_is_good = isinstance(var, classinfo1) or isinstance(var, classinfo2) or isinstance(var, classinfo3) or ... or isinstance(var, classinfoN) It seems silly I can't just pass in a list of classinfo's: var_is_good…
D Adams
  • 2,058
  • 3
  • 16
  • 33
53
votes
4 answers

PHP check for instance of DateTime?

Is this the only way to check if an object is an instance of a class, in my case of the DateTime class? $cls = ReflectionClass("DateTime"); if (! $cls->isInstance( (object) $var ) ) { // is not an instance } It seems a bit heavy to me.
Niklas R
  • 14,369
  • 23
  • 82
  • 179
53
votes
6 answers

Comparing boolean and int using isinstance

Can someone give me an explanation why isinstance() returns True in the following case? I expected False, when writing the code. print isinstance(True, (float, int)) True My guess would be that its Python's internal subclassing, as zero and one -…
jake77
  • 1,364
  • 2
  • 11
  • 19
48
votes
7 answers

How to check if an object is an instance of a namedtuple?

How do I check if an object is an instance of a Named tuple?
Sridhar Ratnakumar
  • 68,948
  • 61
  • 139
  • 172
32
votes
3 answers

Negative form of isinstance() in Python

How would I use a negative form of Python's isinstance()? Normally negation would work something like x != 1 if x not in y if not a I just haven't seen an example with isinstance(), so I'd like to know if there's a correct way to used negation…
mrl
  • 1,028
  • 2
  • 10
  • 20
22
votes
5 answers

check type within numpy array

I have different types of data. most of them are int and sometimes float. The int is different in size so 8/ 16/ 32 bits are the sizes. For this situation I'm creating a numerical type converter. therefore i check the type by using isinstence().…
Jan-Bert
  • 761
  • 3
  • 8
  • 21
22
votes
2 answers

Python: why can isinstance return False, when it should return True?

I'm currently in pdb trace to figure this out ipdb> isinstance(var, Type) False ipdb> type(var) ipdb> Type Why can this happen? P. S. isinstance(var, type(var)) returns True as expected
evgeniuz
  • 2,329
  • 5
  • 26
  • 35
20
votes
5 answers

Checking if an annotation is of a specific type

I am using reflection to see if an annotation that is attached to a property of a class, is of a specific type. Current I am doing: if("javax.validation.Valid".equals(annotation.annotationType().getName())) { ... } Which strikes me as a little…
Vivin Paliath
  • 87,975
  • 37
  • 202
  • 284
19
votes
1 answer

collections.Iterable vs typing.Iterable in type annotation and checking for Iterable

I found that in Python both collections.Iterable and typing.Iterable can be used in type annotation and checking for whether an object is iterable, i.e., both isinstance(obj, collections.Iterable) and isinstance(obj, typing.Iterable) works. My…
Benjamin Du
  • 563
  • 6
  • 13
19
votes
3 answers

python isinstance vs hasattr vs try/except: What is better?

I am trying to figure out the tradeoffs between different approaches of determining whether or not with object obj you can perform action do_stuff(). As I understand, there are three ways of determining if this is possible: # Way 1 if…
Felix
  • 1,764
  • 1
  • 16
  • 27
17
votes
2 answers

isinstance without importing candidates

We have a function which takes a variety of different types of input: a function, a string, a compiled regular expression, a Hamcrest Matcher, and filters a list appropriately based on the type of the input. We're currently using…
Dragon Dave
  • 547
  • 5
  • 15
15
votes
2 answers

"Protocols cannot be used with isinstance()" - why not?

The new typing module contains several objects with names like "SupportsInt" (-Float, -Bytes, etc.). The name, and the descriptions on the documentation page for the module, might be read to suggest that you can test whether an object is of a type…
Hammerite
  • 19,804
  • 4
  • 65
  • 82
15
votes
4 answers

Python: Check if an object is a list of strings

How to check if an object is a list of strings? I could only check if an object is string as such: def checktype(obj): if isinstance(obj,str): print "It's a string" obj1 = ['foo','bar','bar','black','sheet'] obj2 = [1,2,3,4,5,'bar'] obj3 =…
alvas
  • 94,813
  • 90
  • 365
  • 641
13
votes
1 answer

Why is not 'decimal.Decimal(1)' an instance of 'numbers.Real'?

I try to check if a variable is an instance of a number of any type (int, float, Fraction, Decimal, etc.). I cam accross this question and its answer: How to properly use python's isinstance() to check if a variable is a number? However, I would…
Delgan
  • 14,714
  • 6
  • 77
  • 119
12
votes
1 answer

How does isinstance work for List?

I'm trying to understand how Python's type annotations work (e.g. List and Dict - not list or dict). Specifically I'm interested in how isinstance(list(), List) works, so that I can create my own custom annotations. I see that List is defined…
c z
  • 4,951
  • 3
  • 27
  • 39
1
2 3
12 13