Aug-27-2019, 05:10 AM
(Aug-26-2019, 11:12 PM)Exsul Wrote: In general, when do you useisinstance()
and when do you use==
?
They accomplish quite a different tasks:
-
==
compares values of two objects for equality Documentation >> The Python Language Reference >> 6. Expressions >> 6.10.1 Value Comparisons-
isinstance([i]object, classinfo[/i])
checks whether object is instance of class (or subclass) (instance of class is fancy way to say what type the object is - int, float, list etc).You use former if you need to check equality:
>>> a = 42 >>> a == 42 True >>> a == 43 False
You use latter for identity. As in this task - you want to eliminate all integers regardless of the value, so you check whether object is integer and act accordingly.
>>> a = 42 >>> isinstance(a, int) True >>> isinstance(a, float) False >>> isinstance(a, (int, float)) True
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.