I'm looking for the proper way to check a type of object that might be an instance of object() now i'm doing this:
def _default_json_encoder(obj):
""" Default encoder, encountered must have to_dict
method to be serialized. """
if hasattr(obj, "to_dict"):
return obj.to_dict()
else:
# object() is used in the base code to be evaluated as True
if type(obj) == type(object()):
return {}
raise TypeError(
'Object of type %s with value of '
'%s is not JSON serializable'
% (type(obj), repr(obj))
)
but
if type(obj) == type(object()):
return {}
is not recommended, unfortunately
if isinstance(obj, object):
return {}
won't work because all object will be evaluated as True.
So type(obj) == type(object()) is the only way ?
objectinstances? Everything in Python 3 is an instance of (a subclass of)object. Do you have actualobject()instances in your data, and if you do, why?isinstance(obj, class), but again, for theobjectclass, that's everything. You can usetype(obj) is objectto disallow any subclasses. I'm puzzled as to why you'd need this however. Why not just useTrueor some dedicated class instead?object()?