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

Python type checking: int vs int64, float vs float64

In Python, I want to check if certain field values in a dataframe are the correct type. So, I've tried like this: t2check = isinstance(df['T2'][0], float) print("t2check={} type={}".format(t2check, type(df['T2'][0]))) the return I got…
Alex P
  • 45
  • 5
2
votes
2 answers

How to use isinstance to test all possible integer types

When working with integers there are multiple types available (e.g. int, numpy.int8, numpy.int16, etc.). If I write a generic function that requires one variable to be an integer how can I test the type against all possible "integer" types within…
tnknepp
  • 5,888
  • 6
  • 43
  • 57
2
votes
1 answer

Modelica: check equality of replaceable package or model

In my Modelica system model, I have a replaceable package (medium, fluid properties) and a replaceable model (pressure loss model). Can I somehow check whether a certain model or package is selected? The following approach does not work, but maybe…
Priyanka
  • 559
  • 3
  • 13
2
votes
2 answers

using isin across multiple columns

I'm trying to use .isin with the ~ so I can get a list of unique rows back based on multiple columns in 2 data-sets. So, I have 2 data-sets with 9 rows: df1 is the bottom and df2 is the top (sorry but I couldn't get it to show both below, it showed…
verdi
  • 35
  • 1
  • 6
2
votes
1 answer

ABC class number from number module

ABC classes are created to check object type and as such they cannot be instantiated. In fact: basestring() throws: TypeError: The basestring type cannot be instantiated However, this does not happen for the ABC number: from numbers import…
Harry C.
  • 41
  • 1
  • 4
2
votes
1 answer

isinstance in composition cant compare values in if

I trying to use isinstance but i get the false return and not the true. My code is below: from CD import CD from DVD import DVD class Catalogo(object): def __init__(self): self.__itens = list() self.cd = CD("ti", "te", "ar","…
2
votes
1 answer

How to test if Cython property is generator?

In IPython, I can see that a property of a Cython class is a generator by simply defining it and then calling: %%cython cdef class SomeCls: property x: def __get__(self): yield 1 The call looks like SomeCls().x # prints…
user1717828
  • 7,122
  • 8
  • 34
  • 59
2
votes
1 answer

Asserting several variables as same type

The problem: I have a function that takes in parameters n and base, and I would like to assure that both of these parameters are in fact integers. So far I've done the following: # Conditions in which the function makes sense. assert isinstance(n,…
RoyM
  • 735
  • 3
  • 14
2
votes
1 answer

Check if self is instance of subclass in python

I have a class called A, with two subclasses B and C. Does the following make sense? Or is there a better way to it? class A(): ... def do_stuff(self): self.do_this() self.do_that() if isInstance(self, B): …
lukehawk
  • 1,423
  • 3
  • 22
  • 48
2
votes
1 answer

Python isinstance() of float failing assertion test

Background: I've been completing a course in machine learning and neural networks, and in the following where I experienced a problem, we're required to compute the cost function. There are two ways to do so by applying a np.multiply and np.sum OR…
Mika
  • 21
  • 1
  • 2
2
votes
1 answer

Pandas isin() output to string and general code optimisation

I am just starting to use python to do some analysis of data at work, so I could really use some help here :) I have a df with African countries and a bunch of indicators and another df with dimensions representing groupings, and if a country is in…
Tytire Recubans
  • 967
  • 10
  • 27
2
votes
3 answers

How do I check that a python value is an instance of a numpy dtype?

How do I check that a given value can be stored in a numpy array? E.g.: import numpy as np np.array(["a","b"]) ==> array(['a', 'b'], dtype='|S1') np.array(["a","b"]) == 1 > __main__:1: FutureWarning: elementwise comparison failed; returning scalar…
sds
  • 58,617
  • 29
  • 161
  • 278
2
votes
2 answers

Pickle in Python, how to check if variable is a string or a pickle?

I send and receive different objects through the network. In my application the server can receive a pickle or a string. Is there any way of knowing if the data I got is the pickle or a string? I tried: class C: pass if __name__ ==…
Brian Brown
  • 3,873
  • 16
  • 48
  • 79
2
votes
2 answers

isinstance python return different values

I am a bit lost about how isinstance() works in Python. I have used the function before, and the behavior was quite clear, until now. A bit of context. I have a class Classifier which has a method set_kernel that can take a string or a Kernel as the…
JomsDev
  • 116
  • 1
  • 9
2
votes
0 answers

How to access global variables that are non-functions? Python

How to access global variables that are non-functions? Currently I'm access globals() and then manually hardcoding the deletion of functions that I know exist in the module, i.e. x, y, z = 1, 2, 3 def f(i, j): return i+j def g(): variables…
alvas
  • 115,346
  • 109
  • 446
  • 738