Determine which class an object belongs to
In a unit test, I wanted to verify that a function returns a
cookielib.LWPCookieJar object. How to do that? In more general: how to figure out the type of an object?
First I tried to figure out the object’s type with
type(obj) but it was “
obj.__class__ .__name__ told me that obj is an ‘LWPCookieJar’. Better.
Finally, here is how I did the unit test:
assert isinstance(obj, cookielib.LWPCookieJar)
Note however that explicit typechecking is often discouraged in favor of duck typing.
Thanks also to Chris on the Python mailing list.