In Python 2 code I had something like:
...
elif type(value) == datetime.date:
return transform_date(value)
elif type(value) == datetime.datetime:
return transform_datetime(value)
...
Now, if I follow advise of avoiding "antipattern", I should replace it with isinstance
.
isinstance(value, datetime.date)
And there will be a problem, because then correctness will depend on which type I check first.
I am well aware of more than 10 years old How to compare type of an object in Python? and What's the canonical way to check for type in Python? , but this question differs in two important ways:
- It's about Python 3, idioms may be different
- I am interested in exact check, because the transformation depends on the exact type (otherwise it will be lossy)
Exact match: For example, obj.__class__
or type(obj)
matches.
As in Python there is only one obvious way to do it, the answer to this question should not depend on opinion that much.