Proper way to check type when object might be an object() instance
<p>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:</p>

<pre><code>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))
        )
</code></pre>

<p>but </p>

<pre><code>if type(obj) == type(object()):
    return {}
</code></pre>

<p>is not recommended, unfortunately</p>

<pre><code>if isinstance(obj, object):
    return {}
</code></pre>

<p>won't work because all object will be evaluated as True. </p>

<p>So <code>type(obj) == type(object())</code> is the only way ? </p>

<p>If you really have instances of <code>object()</code> in your data structure, then you'd use:</p>

<pre><code>type(obj) is object
</code></pre>

<p>to test for direct instances (classes are typically singletons).</p>

<p>However, I'd rethink my data structure, and either use another primitive value (like <code>True</code>), a dedicated singleton, or a dedicated class.</p>

<p>A dedicated singleton could be:</p>

<pre><code>sentinel = object()
</code></pre>

<p>then test for that sentinel:</p>

<pre><code>if obj is sentinel:
    return {}
</code></pre>

python python-3.x types