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
1 answer

Python Check If List Is a Mathematical Set Fast

What is the fastest\ most pythonic way to check if a List is a mathematical set in python? I know the following works: ListInstance = [1,2,3,4,5,6] ListIsMathSet = (len(set(ListInstance)) == len(ListInstance) ) Is there a better/faster way to check…
D A
  • 3,130
  • 4
  • 25
  • 41
2
votes
2 answers

The best way to determine exception type

I have an exception instance and need to execute code depending on it's type. Which way is more clearly - re raise exception or isinstance check? re raise: try: raise exception except OperationError as err: result = do_something1(err) except…
2
votes
1 answer

isinstance in Jython

In CPython, you can check if something is, for example, a list, using isinstance(something, list). This same code doesn't work in Jython, though. In Jython, it throws this exception: TypeError: isinstance(): 2nd arg is not a class.
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
2
votes
1 answer

how to compare sympy datatypes

I'm new to sympy and and would like to check if an argument is a sympy integer or of type mul. normally, you could do this using: if ( isinstance(arg, int): // do stuff I want to do something like: if( isinstance(arg, Integer): // do…
Justice O.
  • 1,213
  • 12
  • 19
2
votes
1 answer

Why PyObject_IsInstance always return 0 in my sample code

I write a sample to learn python, but when call PyObject_IsInstance, this function always return 0. Here's my c code ReadBuf.c #include "Python.h" static PyObject* Test_IsInstance(PyObject* self, PyObject* args){ PyObject* pyTest = NULL; …
Vatel
  • 79
  • 7
2
votes
1 answer

python 2.7 isinstance fails at dynamically imported module class

I'm currently writing some kind of tiny api to support extending module classes. Users should be able to just write their class name in a config and it gets used in our program. The contract is, that the class' module has a function called…
Rafael T
  • 15,401
  • 15
  • 83
  • 144
2
votes
1 answer

What's the pythonic way to handle a heterogenous list of ids and database rows

My situation is that I am writing a function that allows a user to delete records from a database. For efficiency, the function itself must be able to take a list of arguments for batch deletion. In addition, for simplicity, the list can contain…
zashu
  • 747
  • 1
  • 7
  • 22
1
vote
3 answers

isinstance with string representing types

isinstance("my string", "str | int") isinstance("my string", "list") Is there a way to check the type of a variable ("my string" in this case) based on a valid type which is stored as a string? I don't need to import the type, I know it's builtin…
Some Guy
  • 576
  • 1
  • 4
  • 17
1
vote
2 answers

Isinstance slice in Numba jitclass __getitem__

I am using a numba jitclass and would like to make a transformation on the key whenever it is not a slice (but I want to keep the slice functionality). Question: How can I? To give a little context, I would rather write tensor[coord] than…
Louis-Amand
  • 101
  • 7
1
vote
0 answers

Checking types from an optional module

Say I have to check the type of an object before doing something to it. The objects have different APIs so they need to get dispatched to their own function (I can't simply rely on duck typing). Additionally, the objects could be from one of…
KCharlie
  • 118
  • 6
1
vote
2 answers

How do I remove numbers from a df column containing numbers and text (3 or ABC, but not mixtures, ABC123), leaving blank cells?

I have a dataframe where the first column, lets call it: df['Name'], looks like the "actual" column, and Id like to change it to look the "desired" column in order to do operations on following columns. Here are the actual and desired…
1
vote
1 answer

How can I say that if I want to return an operation on a list, but it stays the same when it comes out null?

I have a list-of-list of word groups in Turkish. I want to apply stemming and I found turkishnlp package. Although it has some shortcomings, it often returns the right word. However, when I apply this to the list, I don't want the structure of my…
Merve
  • 43
  • 5
1
vote
1 answer

isinstance with docx.Document class

in a script I want to run certain lines when a variable is of type Document. However this check always returns false, no matter the input. from docx import Document from docx import Document as _Document document = Document('use_this_doc.docx')…
Frits
  • 113
  • 1
  • 8
1
vote
0 answers

How can a variable be a bool and an int at the same time?

I am currently working on understanding types and how to check for them. I'm now stuck on this: input_variable = True print(type(input_variable)) print(isinstance(input_variable, bool)) print(isinstance(input_variable,…
Ezra
  • 11
  • 1
1
vote
0 answers

Python package "dpath" segments.py giving infinite recursion on a simple "isinstance" call

Attempting to use swagger codegen to auto-generate some a client library for a project I'm doing for work. Apparently swagger doesn't natively have a python version but they link to a community supported swagger-py codegen. Installed this with pip,…
Derek1st
  • 63
  • 6