0

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.

user3848207
  • 3,737
  • 17
  • 59
  • 104
  • `type` is useful when you want to know *exactly* what type something has. It's particularly useful when debugging something, or even just trying to understand it. `isinstance` is good when you don't need to know the exact type, as long as it's derived from some particular type (e.g., you don't mind if it inherits the type or is the type itself). – Tom Karzes May 16 '20 at 08:24
  • 5
    Does this answer your question? [What are the differences between type() and isinstance()?](https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance) – David May 16 '20 at 08:25
  • @David, I'm a bit thick. When I read the answers, I get the impression that isinstanceof() is always better than type(). It doesn't answer my question directly. – user3848207 May 16 '20 at 08:29
  • 1
    From [this answer](https://stackoverflow.com/a/28358473/6045800): *Type-checking with `isinstance(obj, Base)` allows for instances of subclasses and multiple possible bases: `isinstance(obj, (Base1, Base2))` whereas type-checking with `type(obj) is Base` only supports the type referenced.* It is not necessarily about better or worse, it is about what your use-case is... – Tomerikoo May 16 '20 at 08:31

2 Answers2

2

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
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
1

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.