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

Why isinstance() doesn't work as expected?

i was following this tutorial on decision trees and I tried to recreate it on my own as python project, instead of a notebook, using spyder. I create different py files where I put different methods and classes, specifically I create a file named…
Porridge
  • 107
  • 2
  • 13
0
votes
0 answers

python isinstance of "user defined data type" and "JSON object"

I am trying to see if any JSON object (with keys and values) is an instance of an interface implemented. I have done the following thing: JSON Data: _json = {"action": "hello", "completed_at": "2020-04-08T00:38:23+05:30", "created_at":…
Neeraj Sonaniya
  • 375
  • 4
  • 13
0
votes
2 answers

How to explain in layman's terms for this function (function, list, in and isinstance)?

I came across this function: def count_list(l): count = 0 for e in l: if isinstance(e, list): count = count + 1 + count_list(e) …
0
votes
1 answer

Metaclass hash, __eq__ and isinstance

I have implemented a class and its metaclass, both defining binary dunder methods including __eq__ and __hash__. The metaclass' __eq__ method should simply instantiate the class and then pass the comparision to the class operator. But when I use…
Torilla
  • 383
  • 5
  • 16
0
votes
0 answers

Sorting a list with int and str elements

So in my class the professor gave us this insertion-based sorting code, but I need it to sort lists that have numbers and text. I've already tried using if isinstance conditions, but it seems the problem is in the while loop def…
Nin
  • 1
0
votes
0 answers

Is there any way for mypy to support instance checking from within a function?

Is there any way for mypy to support instance checking from within a function? I am aware of the isinstance support for complex type checking. I have the following module as minimal example for illustration purposes: from typing import Any,…
sh0rtcircuit
  • 445
  • 3
  • 13
0
votes
2 answers

What is the canonical way of checking if two objects are of one specific class

Is there a pythonic way to check if two objects are of one specific type? I suppose this is more of academic interest than actual code necessity. Obviously using two isinstance() would work, and the triple equation does the trick as well, but I was…
culicidae
  • 61
  • 1
  • 4
0
votes
2 answers

Returning True if all elements in a user’s list are a list

Solved My exercise is to write a function called list_check, which accepts a list from the user and returns True if every element in the userlist is itself also a list. The bottom line is I would like to see a working example of this problem solved…
0
votes
0 answers

How to change behaviour of isinstance in python

I am working with a large simulation code and am having a hard time executing some iteration. I have imported data of size 262144 from a text file and reshaped into a (64,64,64) set as such filename = path + 'density' file = open(filename,…
APMATH24
  • 1
  • 1
0
votes
1 answer

Use of isinstance(variable,data_type_name) function for NoneType data type gone wrong

Suppose a variable is defined: a=None On running below code: print(isinstance(a,NoneType)) gave error: NameError: name 'NoneType' is not defined For any other data type, its working correctly print(isinstance(5,int), isinstance(4.0,float),…
0
votes
2 answers

isInstance() on same class returns false during exception handling

I am new to java and was trying to implement the isInstance() in a particular exception handling scenario. try { .... ..//some condition throws MyException(); // --> MyException extends RuntimeException }catch(Exception…
Soumav
  • 385
  • 1
  • 6
  • 25
0
votes
3 answers

How to tell if a variable is a specific type of dictionary? i.e. dict(int, str)

I have a dictionary - d = dict( 0='a', 1='b', 2='c' ) How can I tell if d is a dict of type (int, str)? In C# it'd be something like: d.GetType() == typeof(Dictionary)
Stephen K
  • 697
  • 9
  • 26
0
votes
1 answer

use isinstance() cannot check python object class import other module

class A: somemethod class A in module m1 in module m2 I want to use isinstance() to check object obj1 is or not class A and obj1 = A() but isinstance(obj1,A) is False....type(obj1) == I can't understand why? help me
0
votes
4 answers

Python isinstance not working with if statement.

I have a list of addresses. Some address have the street, city, state, zip and others just have city, state, and zip. I made a for loop to put each element into a separate variable. The problem is I am not getting the right output so I put an if…
Kamikaze_goldfish
  • 856
  • 1
  • 10
  • 24
0
votes
1 answer

Python isinstance returns false for types with the same ID

I am having a strange issue with this code in cpuset. For some reason isinstance('/user', str) is returning False. I added some logging like this: log.debug("repr(name): %s", repr(name)) log.debug("type(name): %s", type(name)) …
Timmmm
  • 88,195
  • 71
  • 364
  • 509