title: Python type() or __class__, == or is
body: <p>I want to test whether an object is an instance of a class, and only this class (no subclasses). I could do it either with:</p>
  
  <pre><code>obj.__class__ == Foo
  obj.__class__ is Foo
  type(obj) == Foo
  type(obj) is Foo
  </code></pre>
  
  <p>Are there reasons to choose one over another? (performance differences, pitfalls, etc)</p>
  
  <p>In other words: a) is there any practical difference between using <code>__class__</code> and <code>type(x)</code>? b) are class objects always safe for comparison using <code>is</code>?</p>
  
  <hr>
  
  <p><strong>Update:</strong> Thanks all for the feedback. I'm still puzzled by whether or not class objects are singletons, my common sense says they are, but it's been really hard to get a confirmation (try googling for "python", "class" and "unique" or "singleton").</p>
  
  <p>I'd also like to clarify that, for my particular needs, the "cheaper" solution that just works is the best, since I'm trying to optimize the most out of a few, specialized classes (almost reaching the point where the sensible thing to do is to drop Python and develop that particular module in C). But the reason behind the question was to understand better the language, since some of its features are a bit too obscure for me to find that information easily. That's why I'm letting the discussion extend a little instead of settling for <code>__class__ is</code>, so I can hear the opinion of more experienced people. So far it's been very fruitful!</p>
  
  <p>I ran a small test to benchmark the performance of the 4 alternatives. The profiler results were:</p>
  
  <pre><code>               Python  PyPy (4x)
  type()    is   2.138   2.594
  __class__ is   2.185   2.437
  type()    ==   2.213   2.625
  __class__ ==   2.271   2.453
  </code></pre>
  
  <p>Unsurprisingly, <code>is</code> performed better than <code>==</code> for all cases. <code>type()</code> performed better in Python (2% faster) and <code>__class__</code> performed better in PyPy (6% faster). Interesting to note that <code>__class__ ==</code> performed better in PyPy than <code>type() is</code>.</p>
  
  <hr>
  
  <p><strong>Update 2:</strong> many people don't seem to understand what I mean with "a class is a singleton", so I'll ilustrate with an example:</p>
  
  <pre><code>&gt;&gt;&gt; class Foo(object): pass
  ...
  &gt;&gt;&gt; X = Foo
  &gt;&gt;&gt; class Foo(object): pass
  ...
  &gt;&gt;&gt; X == Foo
  False
  &gt;&gt;&gt; isinstance(X(), Foo)
  False
  &gt;&gt;&gt; isinstance(Foo(), X)
  False
  
  &gt;&gt;&gt; x = type('Foo', (object,), dict())
  &gt;&gt;&gt; y = type('Foo', (object,), dict())
  &gt;&gt;&gt; x == y
  False
  &gt;&gt;&gt; isinstance(x(), y)
  False
  
  &gt;&gt;&gt; y = copy.copy(x)
  &gt;&gt;&gt; x == y
  True
  &gt;&gt;&gt; x is y
  True
  &gt;&gt;&gt; isinstance(x(), y)
  True
  &gt;&gt;&gt; y = copy.deepcopy(x)
  &gt;&gt;&gt; x == y
  True
  &gt;&gt;&gt; x is y
  True
  &gt;&gt;&gt; isinstance(x(), y)
  True
  </code></pre>
  
  <p>It doesn't matter if there are N objects of type <code>type</code>, given an object, only one will be its class, hence it's safe to compare for reference in this case. And since reference comparison will always be cheaper than value comparison, I wanted to know whether or not my assertion above holds. I'm reaching the conclusion that it does, unless someone presents evidence in contrary.</p>