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
-2
votes
2 answers

Error in using operators in function arguments (isinstance) in Python

I can't seem to wrap my head around the output of this function: def is_integer(num1, num2): if isinstance(num1 and num2, int): return 'Yes' else: return 'No' print(is_integer(1.4, 2)) This will output…
PYB
  • 503
  • 6
  • 20
-2
votes
1 answer

How can i pass tuples in isinstance through loop?

My code: def validate_record_schema(): """Validate that the 0 or more Payload dicts in record use proper types""" err_path = "root" try: for record in test1: for device in record.get('Payload', []): …
nrs
  • 937
  • 3
  • 17
  • 42
-3
votes
2 answers

isinstance of double

This might be a silly question but I read somewhere that floats in Python are equal to doubles in C++. So if I want to check whether a variable is a double or not, should I use the following: isinstance(v, float) or this one: isinstance(v, double)
polerto
  • 1,750
  • 5
  • 29
  • 50
-4
votes
3 answers

isInstance not working if the member is in a array

Class X{ Integer x=new Integer(5); Integer y; public static void main (String[] args) throws java.lang.Exception { X i = new X(); String[] str={"x", "y"}; …
Asish
  • 409
  • 2
  • 4
  • 17
-5
votes
1 answer

Does Isinstance Exist in Python Version 3.7?

I am using Visual Studio 2019 to follow along with a python training course which was written in 3.6.1, and am writing in Python version 3.7; however, when I write isinstance, its color does not change, where as the code in the training course does.…
Frasher Gray
  • 222
  • 2
  • 14
1 2 3
15
16