If you write like this:
if type(x) == str:
print("This is a string")
it might work, but it breaks on subclasses of str.
It's better to use
isinstance(). It takes into account inheritance and is more consistent with polymorphism.if isinstance(x, str):
print("This is a string")
This variant will work for str and its subclasses.
Conclusion:
type(x) == str is only suitable for simple cases, but it's fragile. isinstance(x, str) is a more stable and correct option almost always.https://t.me/pythonRe 🤩
