0

Here's an example

import wtforms

isinstance(wtforms.StringField, wtforms.Field)

Why isinstance returns False? If wtforms.StringField is inherited from the wtforms.Field, because in this case should return isinstanse True. Why it returns False? How to fix this?

1 Answers1

1

isinstance checks if object is an instance of class type. In you case, wtforms.StringField is a class type (which can act like an object because Python has first-class everything), and has type type. You actually need issubclass for that.

I.e.:

>>> isinstance(OrderedDict(), dict)
True
>>> isinstance(OrderedDict, dict)
False
>>> isinstance(OrderedDict, type)
True
>>> issubclass(OrderedDict, dict)
True

OrderedDict is derived from dict. Thus, object of OrderedDict() is instance of dict, and OrderedDict is subclass of dict.

Community
  • 1
  • 1
myaut
  • 11,174
  • 2
  • 30
  • 62