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
14
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
  • 18,571
  • 11
  • 90
  • 141
13
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
  • 7,726
  • 3
  • 46
  • 59
11
votes
0 answers

Why does isinstance require a tuple instead of any iterable?

In the following snippet: In [1]: x = [0] In [2]: isinstance(x, list) Out[2]: True In [3]: isinstance(x, (list, set)) Out[3]: True In [4]: isinstance(x, [list,…
pzelasko
  • 2,082
  • 1
  • 16
  • 24
10
votes
3 answers

Should `isinstance()` check against typing or collections.abc?

Both typing and collections.abc includes similar type such as Mapping, Sequence, etc. Based on the python documentation, it seems that collections.abc is preferred for type checking: This module provides abstract base classes that can be used to…
auzn
  • 613
  • 3
  • 14
9
votes
4 answers

Python duck-typing for MVC event handling in pygame

A friend and I have been playing around with pygame some and came across this tutorial for building games using pygame. We really liked how it broke out the game into a model-view-controller system with events as a go-between, but the code makes…
Petriborg
  • 2,940
  • 3
  • 28
  • 49
9
votes
0 answers

Why are instances of old style classes instances of `object`?

In Python 2, why are instances of old style classes still instances of object even when they do not explicitly inherit from object? class OldClass: pass >>> isinstance(OldClass(), object) True Before testing this, I would have concluded that…
Billy
  • 5,179
  • 2
  • 27
  • 53
9
votes
2 answers

Why doesn't Python's `except` use `isinstance`?

The Python documentation for except says: For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
8
votes
2 answers

isinstance of bool?

in Python, i'd like to check to make sure a command line argument is of type bool before I use it in a conditional statement. this: isinstance(sys.argv[2], bool) is coming back false. What's the right way to do this?
Ramy
  • 20,541
  • 41
  • 103
  • 153
8
votes
1 answer

List of classinfo Types

All I'm looking for is a list of possible values of classinfo since the documentation doesn't provide one and I can't seem to find one anywhere else online, let alone SO.
oldboy
  • 5,729
  • 6
  • 38
  • 86
8
votes
2 answers

Proper use of `isinstance(obj, class)`

As I write it, it seems almost surreal to me that I'm actually experiencing this problem. I have a list of objects. Each of these objects are of instances of an Individual class that I wrote. Thus, conventional wisdom says that isinstance(myObj,…
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
7
votes
0 answers

TypeError: isinstance() arg 2 must be a type or tuple of types

If you're like me, you've probably tried: isinstance(my_var, [list, tuple]) only to get: TypeError: isinstance() arg 2 must be a type or tuple of types Now I fully understand the issue (the error message is clear) and how to fix it. I also…
Hans Bouwmeester
  • 1,121
  • 1
  • 17
  • 19
7
votes
2 answers

__instancecheck__ - overwrite shows no effect - what am I doing wrong?

I'm trying to make my class appear as a different object to circumvent lazy type checking in a package I'm using. More specifically, I'm trying to make my object appear as an instance of another object (tuple in my case) when in reality it is not…
Nearoo
  • 4,454
  • 3
  • 28
  • 39
6
votes
2 answers

Is there any value in Python for which isinstance(value, object) is not True?

It is my understanding that since type/class unification every value is of a type that derives from object. However I can't find absolute confirmation of this in the docs. While it stands to reason that isinstance(anything, object) should always be…
maaku
  • 163
  • 4
6
votes
1 answer

Pytest isinstance of a type

I'm pretty new to Python and I'm setting up a little game and I want to test it. Currently, I'm generating an array of objects (Rock, Paper, Scissors) and each of them inherit from a Roll object: def build_the_three_rolls(): return [Rock(),…
noloman
  • 11,411
  • 20
  • 82
  • 129
6
votes
1 answer

issubclass of abstract base class Sequence

This list shows what methods you need to implement for your class to be "regarded" as Sequence: __getitem__, __len__, __contains__, __iter__, __reversed__, index, and count. So why does this minimal implementation does not work, i.e. why…
Kijewski
  • 25,517
  • 12
  • 101
  • 143
1
2
3
15 16