From what I read googling, it seems that isinstanceof()
is always better than type()
.
What are some situations when using type() is better than isinstanceof() in python?
I am using python 3.7.
From what I read googling, it seems that isinstanceof()
is always better than type()
.
What are some situations when using type() is better than isinstanceof() in python?
I am using python 3.7.
They do two different things, you can't really compare them directly. What you've probably read is that you should prefer isinstance
when checking the type of an object at runtime. But that isn't the only use-case for type
(that is the use-case for isinstance
, as its name implies).
What may not be obvious is that type
is a class. You can think of "type" and "class" as synonymous. Indeed, it is the class of class objects, a metaclass. But it is a class just like int
, float
, list
, dict
etc. Or just like a use-defined class, class Foo: pass
.
In its single argument form, it returns the class of whatever object you pass in. This is the form that can be used for type-checking. It is essentially equivalent to some_object.__class__
.
>>> "a string".__class__
<class 'str'>
>>> type("a string")
<class 'str'>
Note:
>>> type(type) is type
True
You might also find this form useful if you ever wanted access to the type of an object itself for other reasons.
In its three-argument form, type(name, bases, namespace)
it returns a new type object, a new class. Just like any other type constructor, just like list()
returns a new list.
So instead of:
class Foo:
bar = 42
def __init__(self, val):
self.val = val
You could write:
def _foo_init(self, val):
self.val = val
Foo = type('Foo', (object,), {'bar':42, '__init__': _foo_init})
isinstance
is a function which checks if... an object is an instance of some type. It is a function used for introspection.
When you want to introspect on the type of an object, usually you will probably use isintance(some_object, SomeType)
, but you might also use type(some_object) is SomeType
. The key difference is that isinstance
will return True
if some_object.__class__
is precisely SomeType
or any of the other types SomeType
inherits from (i.e. in the method resolution order of SomeType
, SomeType.mro()
).
So, isinstance(some_object, SomeType)
is essentially equivalent to some_object.__class__ is SomeType or some_object.__class__ in SomeType.mro()
Whereas if you use type(some_object) is SomeType
, you are only asking some_object.__class__ is SomeType
.
Here's a practical example of when you might want to use type
instead of isinstance
, suppose you wanted to distinguish between int
and bool
objects. In Python, bool
inherits from int
, so:
>>> issubclass(bool, int)
True
So that means:
>>> some_boolean = True
>>> isinstance(some_boolean, int)
True
but
>>> type(some_boolean) is int
False
type says the type of variable:
a = 10
type(a)
It will give its type as 'int'
isinstance() says if variable is related to specified type
class b:
def __init__(self):
print('Hi')
c = b()
m = isinstance(c, b)
It will return True because object c is of class type a otherwise it will return False.