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
1
vote
3 answers

How to separate a list into two lists according to the objects' types using isinstance()?

With a list like this: ["apple", "orange", 5, "banana", 8, 9] How can I put the string(str) in one list and put integer(int) in another list using isinstance()?
PypypieYum
  • 37
  • 5
1
vote
3 answers

How to check if a variable is an instance of mpfr in Python 3?

I want to know how to check whether a variable is of type mpfr or not, this might sound trivial but a simple isinstance(v, mpfr) can't do the trick. Example: create a variable that is an instance of mpfr, how to verify said variable is an instance…
Ξένη Γήινος
  • 2,181
  • 1
  • 9
  • 35
1
vote
1 answer

Why is the isinstance() function not working?

For example: def test(arg = False): return arg, type(arg), isinstance(arg, int) print(test()) will result in: False, The arg variable is False which is obviously a boolean. The type() function got it right but why does…
Tony Stark
  • 23
  • 6
1
vote
0 answers

Pytest raises not working for custom exception

I have the following defined in an exceptions.py file: class Error(Exception): """Base exception raised by api wrapper""" def __init__(self, message: str): self.message = message super().__init__(self.message) # HTTP…
1
vote
3 answers

Iterate through data frame

My code pulls a dataframe object and I'd like to mask the dataframe. If a value <= 15 then change value to 1 else change value to 0. import pandas as pd XTrain = pd.read_excel('C:\\blahblahblah.xlsx') for each in XTrain: if each <= 15: each =…
St. Jimmy
  • 73
  • 8
1
vote
1 answer

isinstance behavior incorrect?

Here is the output from debug console self.functionList = [regression(2)] self.functionList Out[1]: [] type(self.functionList) Out[2]: list isinstance(type(self.functionList), list) Out[3]: False type(self.functionList)…
Alok
  • 3,160
  • 3
  • 28
  • 47
1
vote
1 answer

Problem with checking if input is integer (very beginner)

I'm just starting to learn Python (it's my first language). I'm trying to make a simple program that checks if the number the user inputs is an integer. My code is: number = input('Insert number: ') if isinstance(number, int): …
rage
  • 35
  • 6
1
vote
0 answers

isinstance() does not work inside a function?

When I query isinstance() in the console it works True , but when I use it in a function it does not work False !?? Why does this happen ? In [104]: type(ab) Out[104]: isdp.iSDP In [105]: isinstance(ab, iSDP) Out[105]: True In [106]:…
sten
  • 7,028
  • 9
  • 41
  • 63
1
vote
5 answers

How to determine if variable is int or list for loop in Python

I want to write a function that takes either an int or a list of int as parameter and processes that values. something like that: def func(input): if isinstance(input, int): print(input) else: for i in input: print(i) this doesn't…
1
vote
1 answer

python isinstance in loop nested

What is the most efficient way for python to drill down a nested list of 'PyPDF2.generic.Destination' and list objects to get to the last instance of a list? (to get to all levels of a PDF outline and its page number). pdfread =…
Margosia
  • 27
  • 4
1
vote
1 answer

isinstance alternative to check classes which return flase on being the parent or subclass

Let me first show how isinstance() works class superclass: def __init__(self, var): self.var = var class subclass(p): pass obj = subclass("pinoy") This is how isinstance works >>> isinstance (obj, superclass) True Here, obj is…
Artaza Sameen
  • 519
  • 1
  • 5
  • 13
1
vote
1 answer

Specify the type of input in a function

I was wondering if it was possible to specify the type of input in a function or if I had to use something else. Imagine that I defined a function (example). I want my parameter (type) to indicate what type the input will be as if int(input()) If…
akaSIedge
  • 11
  • 1
1
vote
1 answer

Python code equivalent for these two specifications

Having trouble translating the following two specifications to Python code. I'm finding the terminology is strange for a Python program (throw, catch, contents of exception). First specification states "If the file can't be opened because it does…
Grog
  • 13
  • 2
1
vote
2 answers

monkey patching for duck typing

# python3.7 Python 3.7.2 (default, Feb 15 2019, 16:54:46) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from collections.abc import * >>> from _collections_abc import…
lyu.l
  • 282
  • 3
  • 8
1
vote
4 answers

How to subclass int and use isinstance to identify instances of it?

I want to subclass int (or some other analogous builtin numerical type), that I can explicitly type check. This q&a is similar, but didn't answer what I'm seeing exactly: Sub-classing a built-in Python type such as int Here's a rough example of…
BuvinJ
  • 10,221
  • 5
  • 83
  • 96