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

python instance() or type() cannot get the right result

Your any help would be great. Thanks in advance! I have 2 files, like below file1: import file2 class Foo(): pass if __name__ == '__main__': foo = file2.dummy() print("right?", isinstance(foo, Foo)) print("right?",…
1
vote
2 answers

What is the most efficient manner to handle errors in python?

I'm newbie in python and I'm trying to handle errors but I don't know the most efficient way to handle errors in python. I tried this way, but it seems a bit complex to understand. I think may can exist some other "better" manner to handle it. def…
1
vote
0 answers

Making isinstance fail (in a customizable way)

I'm trying to write a regression test for a Python C extension that "forgot" to check for failures for PyObject_IsInstance. But the only thing I could come up with was a metaclass that overrides __instancecheck__, for example: import six class…
MSeifert
  • 145,886
  • 38
  • 333
  • 352
1
vote
1 answer

Python- isinstance- use of PLUS(+) sign

Can anyone help me in understanding what is the use of plus(+) sign in using isinstance. In [76]: isinstance('qwert', string) Out[76]: True In [77]: isinstance('qwert', string + (int,)) Out[77]: True Especially, this part--> string + (int,)
xlax
  • 2,661
  • 6
  • 12
  • 15
1
vote
1 answer

Why does subclass of datetime not an instance of subclass when specifying argument tz to method fromtimestamp?

Below is sample code that subclasses datetime. Since pass is the only subclass body, method '__new__' of datetime is expected to be preserved. The following code has been tested on Python 3.4.2 on Mac OS 10.12.3 and Python 3.6.0 on Arch Linux. In…
paretech
  • 346
  • 3
  • 8
1
vote
2 answers

How to check data type in C++?

I'm fairly new to C++, I've been mainly using python. I'm trying to check the type of variable of the value stored in the objects I'm working on. I remember that in Python there was a comand isinstance where I could use it as a condition to run…
Twhite1195
  • 351
  • 6
  • 17
1
vote
1 answer

How to check if token is a float?

So I need to modify the following code so that the methods PostfixEval() and infixToPostfix() can take floats, as well as integers with more than one digit. I've tried isinstance(token,float) == True. Maybe I'm not using it correctly. def…
RonNotJohn
  • 11
  • 2
1
vote
1 answer

isinstance() is not behaving as I expect

I have a class I've written, MyEdge (it stores two nodes to make an edge for some graphs), and I'm struggling to figure out why isinstance seems to be behaving inconsistently. I have an object, new_road, that thinks it is in the MyEdge class when…
Christa
  • 497
  • 1
  • 4
  • 8
1
vote
5 answers

How do I check if an input is a string or int in Python 2.x?

I am trying to check if an input is a word or a number. var = input("var: ") if isinstance(var, str): print "var = word" else: print "var = number" This is the code I came up with but sadly doesn't work; I'm new to python and programming…
Manakin
  • 21
  • 1
  • 1
  • 4
1
vote
1 answer

Python: How to use isinstance from parent to determine if it is a specific child

I have a few classes class Parent(): def DoThing(): if isinstance(self, 'child1'): DoSomething() elif: DoSomethingElse() import Parent class Child1(Parent): def DoThing(): #Do some things…
1
vote
2 answers

Using isinstance() versus duck typing

I'm writing an interface to matplotlib, which requires that lists of floats are treated as corresponding to a colour map, but other types of input are treated as specifying a particular colour. To do this, I planned to use…
Kyle_S-C
  • 1,107
  • 1
  • 14
  • 31
1
vote
1 answer

In Python, why isinstance function says that a datetime is an instance of datetime.date?

The problem is that the isinstance function says that datetime.datetime instances are also instances of datetime.date Why? That makes sense? Python 2.7.4 (default, Sep 26 2013, 03:20:26) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or…
matiascelasco
  • 1,145
  • 2
  • 13
  • 18
1
vote
1 answer

isinstance not working correctly with beautifulsoup(NameError)

I'm using isinstance to select some html tags and passing them to a Beautifulsoup function. The problem is I keep getting NameErrors from what should be perfectly executable code. def horse_search(tag): return (tag.has_attr('href') and…
1
vote
1 answer

Python - isinstance(classInfo, classInfo)

The Python documentation says that isinstance requires an instance object of a class and the class info. The problem is: I just have 2 class info object and have to check if class_inf1 is a instance of class_inf2 Example: class Foo(object): …
Matt3o12
  • 4,192
  • 6
  • 32
  • 47
1
vote
5 answers

Python: checking type of variables

I have a question about python. I have variables a, b, c and d. And I have the following line: if not isinstance(a, int) or not isinstance(b, int) \ or not isinstance(c, int) or not isinstance(d, int) \ or not isinstance(a, float) or…
SomeOne
  • 469
  • 2
  • 6
  • 16