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)
230 questions
6
votes
7 answers

How to check a specific type of tuple or list?

Suppose, var = ('x', 3) How to check if a variable is a tuple with only two elements, first being a type str and the other a type int in python? Can we do this using only one check? I want to avoid this - if isinstance(var, tuple): if…
yashu_seth
  • 81
  • 1
  • 4
6
votes
2 answers

Strange behaviour of isinstance function

I have a class named Factor in the module Factor.py (https://github.com/pgmpy/pgmpy/blob/dev/pgmpy/factors/Factor.py) and also have function named factor_product in Factor.py as: def factor_product(*args): if not all(isinstance(phi, Factor) for…
Ankur Ankan
  • 2,953
  • 2
  • 23
  • 38
5
votes
5 answers

Recursion over a list of lists without isinstance()

I have just read "isinstance() considered harmful", and it seems reasonable. In short, it argues for avoiding the use of this function. Well, just now I happen to be writing a program which takes inputs structured as a tree, and needs the tree's…
Emilio M Bumachar
  • 2,532
  • 3
  • 26
  • 30
5
votes
2 answers

Python: Best way to check for same type between two variables

I'm looking to check if two variables are of the same type in python 3.x. What is the most ideal way to do this? Take the following example: class A(): def __init__(self, x): self.x = x class B(A): def __init__(self, x): x…
Liam Rahav
  • 275
  • 1
  • 5
  • 17
5
votes
0 answers

python type() and isinstance() don't recognize instance of class

isinstance(object, classinfo) doesn't recognize an instance of a class. I printed type(object) and the class itself to verify that they were the same types. They both printed the exact same thing however when I tried type(object) == class it…
ecart33
  • 78
  • 8
5
votes
3 answers

is the "is" operator just syntactic sugar for the "IsInstanceOfType" method

Are the following code snippets equivalent? class a {} class b:a {} b foo=new b(); //here it comes foo is a //...is the same as... typeof(a).isinstanceoftype(foo) Or maybe one of the other Type Methods map closer to the is operator. e.g.…
rudimenter
  • 3,242
  • 4
  • 33
  • 46
5
votes
2 answers

detect if variable is of sympy type

I have a variable which may or may not be a sympy class. I want to convert it to a float but I’m having trouble doing this in a general manner: $ python Python 2.7.3 (default, Dec 18 2014, 19:10:20) [GCC 4.6.3] on linux2 >>> import sympy >>> x =…
mulllhausen
  • 4,225
  • 7
  • 49
  • 71
5
votes
1 answer

isinstance behavior with module reload

Given the following two .py files: aclass.py class A(object): pass main.py def importer(klass): """ Used to import classes from there python qalname """ import_ = lambda m, k: getattr(__import__(m, fromlist=k), k) klass =…
ohe
  • 3,461
  • 3
  • 26
  • 50
5
votes
5 answers

When is obj.GetType().IsInstanceOfType(typeof(MyClass)) true?

I'm looking at this piece of code written by someone else, and I'm wondering when it would evaluate to true. Basically, it is saying someType is an instance of someOtherType. Does it even make sense? So far, I've…
user1486691
  • 267
  • 5
  • 18
5
votes
2 answers

instanceof vs isInstance()

class A{ public A(){ System.out.println("in A"); } } public class SampleClass{ public static void main(String[] args) { A a = new A(); System.out.println(A.class.isInstance(a.getClass())); …
Jaikrat
  • 1,124
  • 3
  • 24
  • 45
4
votes
0 answers

Python 3: TypeError: Subscripted generics cannot be used with class and instance checks

How do I test for subtypes in both Python 2 and Python 3? In Python 2.7.18: >>> import typing >>> type_ = typing.List[str] >>> issubclass(type_, typing.List) True But in Python 3.9.10, I get: TypeError: Subscripted generics cannot be used with…
cclauss
  • 552
  • 6
  • 17
4
votes
2 answers

How does isinstance work in python for subclasses?

Playing around with isinstance, I would like to know how it works for a subclass check. class A: pass class B(A): pass class C(B): pass print(isinstance(C(), C)) # True, I understand this fine print(isinstance(C(), B)) # True, Ok, but…
Alice
  • 115
  • 6
4
votes
0 answers

Is there a way to mock isinstance() of an object in python?

I want to write a mock for a library object without inheriting from it in order to properly test, however without having to stub all non used functions of the original object. To be specific I want to write a ContextMock for the invoke…
Manuel S.
  • 63
  • 5
4
votes
2 answers

python datetime instance check with date returns true

I have a 2 objects, one is datetime and the other is date object. When I want to check the type of object using isinstance(python's preferred way), I get a little absurd results >>> from datetime import date, datetime >>> a=date.today() >>>…
newbie
  • 1,282
  • 3
  • 20
  • 43
4
votes
3 answers

isinstance(False, int) returns True

I want to determine if a variable is an integer, so I use the following code: if isinstance(var, int): do_something() but when var = False, the do_something function is executed. when var = None, the isinstance() function works normaly.
1 2
3
15 16