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

How to create a class decorating another class using python

My goal is to implement a classic Decorator pattern (not functional @decorator, provided by python from the box). But I don't want to implement every method and attribute in decorator class. I want to make it in a more universal and elegant way. My…
M1ha Shvn
  • 103
  • 5
0
votes
1 answer

In Python, how to get typing right when using a class decorator?

This is my first attempt to make a Python decorator, and I humbly admit to drawing inspiration from a number of StackOverflow posts while doing so. When I scavenged the __instancecheck__ method, the last two asserts stopped triggering, but now…
0
votes
0 answers

Check if input is Tuple[int, int] or Iterable[Tuple[int, int]] in Python

This might be easy but I'm not sure how to do that. I have a method looking like this: def my_method( self, coords: Union[Tuple[int, int], Iterable[Tuple[int, int]] ): ... that is, my_method needs a tuple of integers or something like a…
schoeni
  • 65
  • 9
0
votes
1 answer

np.isin - testing whether a Numpy array contains a given row considering the order

I am using the following line to find if the rows of b are in a a[np.all(np.isin(a[:, 0:3], b[:, 0:3]), axis=1), 3] The arrays have more entries along axis=1, I only compare the first 3 entries and return the fourth entry (idx=3) of a. The…
zeniapy
  • 175
  • 2
  • 12
0
votes
1 answer

Is isinstance() function not enough to detect integers and floats?

I'm making a very basic calculator as my first project (I'm learning Python and want something to show what i've learned so far) and as it is right now, the program works fine. As it is right now, I have a function to validate user inputs so that…
Santi
  • 5
  • 3
0
votes
4 answers

Is there any way to find whether a value in string is a float or not in python?

I have a problem with string, I am aware that using isdigit() we can find whether an integer in the string is an int or not but how to find when its float in the string. I have also used isinstance() though it didn't work. Any other alternative for…
sirisha
  • 73
  • 1
  • 8
0
votes
1 answer

Is a type assigned to pickled objects?

I am using Python 2.7 to parse a multi-layered list of GPS coordinates for plotting. The list is parsed recursively, having taken it from sqlite3, and at some point in the process hits a coordinate set that has been pickled. I'm needing a way of…
Evan Jones
  • 19
  • 3
0
votes
2 answers

Which is the equivalent of "isinstance" (Python) for Java?

I have a program to generate requests to test Apis, the next part of the code reads a JSON to manage the info of the values later: def recorre_llaves(self, dicc): campos = [] for k in dicc.keys(): if isinstance(dicc[k], dict): …
Daniel Lopez
  • 63
  • 1
  • 6
0
votes
2 answers

Isinstance function to check the elements given fits in my matrix

I want to create a function that checks if the given tab fits or not. My matrix should be a 3x3 with 3 tuples, each with 3 integers. I want this interaction to happen: >>> tab = ((1,0,0),(-1,1,0),(1,-1,-1)) >>> tabuleiro(tab) True >>> tab =…
Cate C
  • 31
  • 6
0
votes
1 answer

How to check whether a numpy.array exists or not ? Truth testing value for numpy array?

While developing a class that uses numpy.array, I'd like to construct this array at one stage, and later check whether it exists in order to manipulate it, or construct it at a later stage (pseudo-code below). How could I check that this array…
FraSchelle
  • 249
  • 4
  • 10
0
votes
1 answer

Check if an item is an instance but not a subclass

How can you check if an object is an instance of a class but not any of its subclasses (without knowing the names of the subclasses)? So if I had the below code: def instance_but_not_subclass(object, class): #code return result class…
Linden
  • 531
  • 3
  • 12
0
votes
1 answer

module 'odoo.tools.pycompat' has no attribute 'integer_types' odoo 13

i have an code related to pycompat library in odoo 12 when i tried to upgrade this code in odoo 13 it gives me below error: if isinstance(res_ids, pycompat.integer_types): AttributeError: module 'odoo.tools.pycompat' has no attribute…
Pawan Kumar Sharma
  • 1,168
  • 8
  • 30
0
votes
1 answer

How do I correctly use isinstance() in my random number guessing game or is another function needed?

I want this number guessing game to be able to catch every possible exception or error the user enters. I've successfully prevented the use of strings when guessing the number, but I want the console to display a custom message when a float is…
0
votes
1 answer

Why is a pandas.DatetimeIndex also identified as a pd.Int64Index?

Even though the single-level index of my dataframe shown below is clearly a pd.DatetimeIndex, it is still also identified as a pd.Int64Index. In what follows, I demonstrate this behavior via printouts in the VS Code debugging…
Andreas L.
  • 3,239
  • 5
  • 26
  • 65
0
votes
3 answers

How do you check if a variable is numeric (without isnumeric) and raise error if it is not in Python?

I don't understand why the following does not operate correctly and raises errors when radius or height are float numbers. def cone(radius, height): if isinstance(radius, int) or isinstance(radius,float) == False: raise…
Elle
  • 75
  • 1
  • 6