1

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?

Alok
  • 3,160
  • 3
  • 28
  • 47
  • 1
    ``isinstance(type(self.functionList), list)`` checks whether the *type* of ``self.functionList`` is an instance of ``list``. Only ``self.functionList`` *itself* is an instance of ``list``. ``isinstance(self.functionList, list)`` should be ``True``. – MisterMiyagi Oct 11 '20 at 09:50
  • Does this answer your question? [How do you set a conditional in python based on datatypes?](https://stackoverflow.com/questions/14113187/how-do-you-set-a-conditional-in-python-based-on-datatypes) – MisterMiyagi Oct 11 '20 at 09:59
  • thanks! That was rather a silly mistake on my side. – Alok Oct 11 '20 at 12:54

1 Answers1

1

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.

Mithilesh_Kunal
  • 873
  • 7
  • 12