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
1 answer

How to catch integers in dictionary keys, most of them string, but some is integers that I need to remove

How to catch integers in dictionary keys, most of them string, but some is integers that i need to remove. I tried this: def print_words(filename): dict = create_dict(filename) for key, val in sorted(dict.items()): # Integer filter…
0
votes
1 answer

isinstance raising typeError when used on multiprocessing Lock

I'm using the multiprocessing module on Python3.4. For some reason I get the following error using isinstance(): >>> from multiprocessing import Lock >>> isinstance(Lock(), Lock) Traceback (most recent call last): File "", line 1, in…
Jad S
  • 2,705
  • 6
  • 29
  • 49
0
votes
5 answers

isinstance with tee objects

As a part of memoizing mechanism, I have a generator, I tee it, and I need to check its type with isinstnace. >>> gen = (i for i in range(5)) >>> one, two = itertools.tee(gen, 2) then >>> type(one), type(two) (itertools.tee, itertools.tee) as…
meto
  • 3,425
  • 10
  • 37
  • 49
0
votes
2 answers

How to make general type checker in python

I got some similar code to this. Not exacly, but I don't to put to much, but I need that check_type accept r_type parameter as string and check if object type is of value of this string. It is doable?!?!? I repeat can't do that:…
emcek
  • 459
  • 1
  • 6
  • 17
0
votes
1 answer

Is usage of isinstance is justified when calling different methods by passed object

I'm working on little tool that is able to read file's low level data i.e mappings, etc, and store the results to sqlite DB, using python's builtin sqlite API. For parsed file data I have 3 classes: class GenericFile: # general file class #…
Samuel
  • 3,631
  • 5
  • 37
  • 71
0
votes
0 answers

isinstance gets stuck (or infinite loop?) in __init__ from my class python

So I have a class Panini, which I have to initialize, but it keeps getting stuck in the line assert isinstance(stickers, (int, tuple, set, list)), 'invalid stickers' and when I remove the line it gets stuck in the next isinstance, can somebody tell…
user5609174
0
votes
0 answers

python isinstance gives different results in shell and wsgi

I have encountered a strange problem in which the same object(agent), when checking if it is an instance of a class(Agents), returns True in the shell, but False in the wsgi server. in the python shell: In [78]: agent = Agents.by_id(566) #…
Renier
  • 1,523
  • 4
  • 32
  • 60
0
votes
1 answer

MutableSequence to pass as a list in isinstance() check

I built a custom list-like class based on collections.MutableSequence: class MyList(collections.MutableSequence): etc... behave mostly like a list... value = MyList([1,2,3]) Before processing list data, a third-party library runs a check: def…
Arne Wolframm
  • 367
  • 4
  • 16
0
votes
2 answers

isinstance not working when list is used to check in Python

I am trying to load a text file that has two column data, separated by a tab. The first column values could be either integers or floats, while the second column will always be floats. Now, I am using isinstance to see if my first column is integer…
hypersonics
  • 1,064
  • 2
  • 10
  • 23
0
votes
1 answer

Error in Python when using JSON.Load : name 'isinstance' is not defined

Python noob here. I'm trying to execute a Python script but on json.load it fails with the error message name 'isinstance' is not defined. Since json is a library that comes with the Python installation (3.4.2) I find this very strange. Has anyone…
Z317
  • 1
0
votes
1 answer

How to judge a file type both work in python 2 and python 3

Because file can't use in python 3. In python 2, we judge a file type can do this: with open("xxx.txt") as f: if isinstance(f, file): print("ok!") In python 3, we can do this: import io with open("xxx.txt") as f: if isinstance(f,…
Aleeee
  • 1,913
  • 6
  • 17
  • 27
0
votes
1 answer

What is the right direction of using "*.isInstance"?

I am confused every time I read the Java Documentation again to that. So please try to help me in your own words. List list = new ArrayList(); //Child extends Parent... list.add(new Child()); ... ... for(Parent p: list){ …
Oekel
  • 43
  • 9
0
votes
2 answers

Understanding isinstance in recursion

So basically I am having trouble understanding these 2 concepts. I've googled for 2 days and played around with those 2 to create some kind of a picture for myself but its still like I don't truly understand everything. As much as I understand,…
0
votes
1 answer

Read and parsing file content in python

I have a file which contain entries in the following format: FID=COST|Tolerance=1 FID=(BUY,SELL)|Tolerance=0 FID=[(X,Y),(A,B)]|Tolerance=0 What would be the best way to find out if FID is a string, tuple or list while reading the file? A normal…
IUnknown
  • 9,301
  • 15
  • 50
  • 76
0
votes
1 answer

Is it possible to get isinstance or similar type-checking to work with my pickled classes?

I have a really annoying bug, and I've finally tracked it down. I use (or more accurately, already have used) a stand-alone script to create a store of objects, after which I pickle this data structure. I can then initialize my main program with…
Brian Peterson
  • 2,800
  • 6
  • 29
  • 36