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

code completion in pydev (eclipse) when the variable type is unknown

for a varible with type unknown (not during runtime), for codecomplete the following statement is working assert isinstance(variable, class) variable. {codecomplete works} while the following statement is not assert isinstance(self.variable,…
0
votes
0 answers

"InfluxDBReader" and "StockPriceObserver" are not defined

@ModelFactory.register_model class OverExtendedGapDown(BaseModel): def __init__(self, data_provider, mode): super().__init__(data_provider) self.data_provider = data_provider self.mode = mode # "backtest" or "live" …
David
  • 13
  • 5
0
votes
0 answers

how can i put a message error if the variable is not an integer

im doing a calculator and im trying to give a message when the typed on the input is not an int rather than the program just closing(on the exe file),it gives an error when running in vscode.can someone help me? this is the code: if window ==…
Renzo-M
  • 21
  • 3
0
votes
0 answers

Amend the classes to include the following functionality

Amend your looking method to create a single string representing the animals and birds in the zoo. Hint: create a static method that accepts the concatenated list of animal list and bird list and uses the reduce and map functions with the lambda…
0
votes
1 answer

How to differentiate model types from xgboost XGBRFClassifier and XGBClassifier

I'm building a module that supports both XGBClassifier and XGBRFClassifier from xgboost. I want to differentiate them in my module but I noticed that: from xgboost import XGBRFClassifier, XGBClassifier xgbrf_model =…
0
votes
1 answer

Type check scipy.stats distributions with isinstance

I have several scipy.stats distributions in a list. I want to check if each distribution is e.g. uniform, normal or something else by isinstance. However, the type of all these distributions seem to be…
0
votes
0 answers

Please advise on the error Traceback (most recent call last)

TypeError Traceback (most recent call last) ~\AppData\Local\Temp\3\ipykernel_5356\2783521920.py in 1 age = 1006 2 ----> 3 isinstance (age, str) TypeError: isinstance() arg 2 must be a type or tuple of types I tried…
0
votes
2 answers

sympy: sympy function symbolic or non-symbolic

I am trying to understand about sympy's symbolic functions: import sympy from sympy.abc import x,y,z with sympy.evaluate(False): print(sympy.sympify("diff(x,x)").func) print(sympy.parse_expr("diff(x, x)",…
user20693776
0
votes
1 answer

Select only a type of object inside a list python

I have a list of 2 types of customers classic customer and hipsters. I have a function associated to these classes which tells me if they are in budget. I would like to know if it still exist a hipster which is in budget. Something like, however the…
Bruce Rv
  • 23
  • 5
0
votes
2 answers

Using isinstance for a list of bools / ints

I have a list of ints, lst = [1, 2, 3] how do I do a isinstance on this, to return True only for a list of ints? isinstance(lst, list[int]) # does not work
apostofes
  • 2,959
  • 5
  • 16
  • 31
0
votes
3 answers

how to check if an object is a certain type in python

Im trying to make a method that checks if the object will be of a faculty type based from the faculty class. This is the code but im getting a syntax error? def addFaculty(self, f_obj): if f_obj isinstance(f_obj, Faculty): return…
0
votes
0 answers

The type of returned objects of simplify_logic

According to the document, simplify_logic returns Or or And Object. Am I using it incorrectly?(Are there any restrictions on arguments?) import sympy from sympy import S from sympy.logic.boolalg import Equivalent,simplify_logic from sympy.abc…
ryoryon66
  • 1
  • 2
0
votes
1 answer

How to exclude a integer that is a string in a json using isinstance python

I have a json that has all values of strings. I have a decoder that takes all the strings and decodes them into the integers, but there is one value that is showing as "01" that I would like to keep as a string. Is it possible to exclude this in the…
Alphanum3ric
  • 39
  • 10
0
votes
1 answer

Python3 -unittest mocking - problem with isinstance

I have some problem with mocking and testing my function/class. I think the easiest way to explain it will be showing some code: module.py: import some_module class MyClassToTest: def __init__(): pass def f(self, a): if…
tomm
  • 271
  • 2
  • 14
0
votes
0 answers

Creating function in Python to check weather a specific coordinate has an instance of a class

We are creating a predator-prey simulation in Python. Classes Patch (with coordinates self.x and self.y where the animals can be located), and classes Animal and Animals subclasses Fox and Rabbit. We need to write a function "has_alive_rabbit". How…