isinstance(list(), type(mylist))
isinstance(mylist, list)
Is there any difference apart from the speed? I can see 2 is faster.
isinstance(list(), type(mylist))
isinstance(mylist, list)
Is there any difference apart from the speed? I can see 2 is faster.
If used correctly, the results will be the same. But you should only use the second version.
There's no need to create an empty object, i.e. list()
, and no reason for an extra function call via type
.
There's also a danger you'll do something like this:
from collections import OrderedDict
array = dict()
res1 = isinstance(OrderedDict(), type(array)) # True
res2 = isinstance(array, OrderedDict) # False
The first one is very inefficient, as you are first creating a new list object, and then identifying the type of my_list
and then checking if they both are same.
The second option is how the isinstance
function is supposed to be used.