Search code examples
python-3.xisinstance

isinstance behavior incorrect?


Here is the output from debug console

self.functionList = [regression(2)]
self.functionList
Out[1]: [<regression at 0x2530370a2c8>]
type(self.functionList)
Out[2]: list
isinstance(type(self.functionList), list)
Out[3]: False
type(self.functionList) == list
Out[4]: True
import typing
isinstance(type(self.functionList), typing.List)
Out[16]: False

I am confused as in why isinstance function returns False even though the variable functionList is clearly an instance of type list.

What is the issue with isinstance behavior?


Solution

  • You are comparing the wrong ones. Try this code.

    import typing
    isinstance(self.functionList, typing.List)
    

    For isinstance method, compare the object with the expected type. For more info, refer the docs.