<p>What is the difference between old style and new style classes in Python?  When should I use one or the other?</p>

[{'answer_id': 54873, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 10, 'selected': True, 'text': '<p>From <em><a href="http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes" rel="noreferrer">New-style and classic classes</a></em>:</p>\n\n<blockquote>\n  <p><strong>Up to Python 2.1, old-style classes were the only flavour available to the user.</strong></p>\n  \n  <p>The concept of (old-style) class is unrelated to the concept of type:\n  if <code>x</code> is an instance of an old-style class, then <code>x.__class__</code>\n  designates the class of <code>x</code>, but <code>type(x)</code> is always <code>&lt;type\n  \'instance\'&gt;</code>. </p>\n  \n  <p>This reflects the fact that all old-style instances, independently of\n  their class, are implemented with a single built-in type, called\n  instance.</p>\n  \n  <p><strong>New-style classes were introduced in Python 2.2 to unify the concepts of class and type</strong>. \n  A new-style class is simply a user-defined type, no more, no less.</p>\n  \n  <p>If x is an instance of a new-style class, then <code>type(x)</code> is typically\n  the same as <code>x.__class__</code> (although this is not guaranteed – a\n  new-style class instance is permitted to override the value returned\n  for <code>x.__class__</code>).</p>\n  \n  <p><strong>The major motivation for introducing new-style classes is to provide a unified object model with a full meta-model</strong>. </p>\n  \n  <p>It also has a number of immediate benefits, like the ability to\n  subclass most built-in types, or the introduction of "descriptors",\n  which enable computed properties.</p>\n  \n  <p><strong>For compatibility reasons, classes are still old-style by default</strong>. </p>\n  \n  <p>New-style classes are created by specifying another new-style class\n  (i.e. a type) as a parent class, or the "top-level type" object if no\n  other parent is needed. </p>\n  \n  <p>The behaviour of new-style classes differs from that of old-style\n  classes in a number of important details in addition to what type\n  returns. </p>\n  \n  <p>Some of these changes are fundamental to the new object model, like\n  the way special methods are invoked. Others are "fixes" that could not\n  be implemented before for compatibility concerns, like the method\n  resolution order in case of multiple inheritance.</p>\n  \n  <p><strong>Python 3 only has new-style classes</strong>. </p>\n  \n  <p>No matter if you subclass from <code>object</code> or not, classes are new-style\n  in Python 3.</p>\n</blockquote>\n'}, {'answer_id': 242700, 'author': 'Halo', 'author_id': 5306, 'author_profile': 'https://Stackoverflow.com/users/5306', 'pm_score': 3, 'selected': False, 'text': '<p>New-style classes inherit from <code>object</code> and must be written as such in Python 2.2 onwards (i.e. <code>class Classname(object):</code> instead of <code>class Classname:</code>). The core change is to unify types and classes, and the nice side-effect of this is that it allows you to inherit from built-in types.</p>\n\n<p>Read <a href="http://www.python.org/download/releases/2.2.3/descrintro/" rel="noreferrer">descrintro</a> for more details.</p>\n'}, {'answer_id': 1203997, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 8, 'selected': False, 'text': "<p><strong>Declaration-wise:</strong></p>\n\n<p>New-style classes inherit from <em>object</em>, or from another new-style class.</p>\n\n<pre><code>class NewStyleClass(object):\n    pass\n\nclass AnotherNewStyleClass(NewStyleClass):\n    pass\n</code></pre>\n\n<p>Old-style classes don't.</p>\n\n<pre><code>class OldStyleClass():\n    pass\n</code></pre>\n\n<p><strong>Python 3 Note:</strong></p>\n\n<p>Python 3 doesn't support old style classes, so either form noted above results in a new-style class.</p>\n"}, {'answer_id': 3228045, 'author': 'xioxox', 'author_id': 351771, 'author_profile': 'https://Stackoverflow.com/users/351771', 'pm_score': 5, 'selected': False, 'text': "<p>Old style classes are still marginally faster for attribute lookup. This is not usually important, but it may be useful in performance-sensitive Python 2.x code:</p>\n\n<pre>\nIn [3]: class A:\n   ...:     def __init__(self):\n   ...:         self.a = 'hi there'\n   ...:\n\nIn [4]: class B(object):\n   ...:     def __init__(self):\n   ...:         self.a = 'hi there'\n   ...:\n\nIn [6]: aobj = A()\nIn [7]: bobj = B()\n\nIn [8]: %timeit aobj.a\n10000000 loops, best of 3: 78.7 ns per loop\n\nIn [10]: %timeit bobj.a\n10000000 loops, best of 3: 86.9 ns per loop\n</pre>\n"}, {'answer_id': 16193572, 'author': 'Xiao Hanyu', 'author_id': 2310396, 'author_profile': 'https://Stackoverflow.com/users/2310396', 'pm_score': 5, 'selected': False, 'text': '<p>Guido has written <em><a href="http://python-history.blogspot.com/2010/06/inside-story-on-new-style-classes.html" rel="nofollow noreferrer">The Inside Story on New-Style Classes</a></em>, a really great article about new-style and old-style class in Python.</p>\n\n<p>Python 3 has only new-style class. Even if you write an \'old-style class\', it is implicitly derived from <code>object</code>.</p>\n\n<p>New-style classes have some advanced features lacking in old-style classes, such as <code>super</code>, the new <a href="http://en.wikipedia.org/wiki/C3_linearization" rel="nofollow noreferrer">C3 mro</a>, some magical methods, etc.</p>\n'}, {'answer_id': 16295402, 'author': 'jamylak', 'author_id': 1219006, 'author_profile': 'https://Stackoverflow.com/users/1219006', 'pm_score': 3, 'selected': False, 'text': '<p>New style classes may use <code>super(Foo, self)</code> where <code>Foo</code> is a class and <code>self</code> is the instance.</p>\n<blockquote>\n<p><code>super(type[, object-or-type])</code></p>\n<p>Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class. The search order is same as that used by getattr() except that the type itself is skipped.</p>\n</blockquote>\n<p>And in Python 3.x you can simply use <code>super()</code> inside a class without any parameters.</p>\n'}, {'answer_id': 19273761, 'author': 'ychaouche', 'author_id': 212044, 'author_profile': 'https://Stackoverflow.com/users/212044', 'pm_score': 5, 'selected': False, 'text': '<p>Here\'s a very practical, true/false difference. The only difference between the two versions of the following code is that in the second version <em>Person</em> inherits from <em>object</em>. Other than that, the two versions are identical, but with different results:</p>\n\n<ol>\n<li><p>Old-style classes</p>\n\n<pre><code>class Person():\n    _names_cache = {}\n    def __init__(self,name):\n        self.name = name\n    def __new__(cls,name):\n        return cls._names_cache.setdefault(name,object.__new__(cls,name))\n\nahmed1 = Person("Ahmed")\nahmed2 = Person("Ahmed")\nprint ahmed1 is ahmed2\nprint ahmed1\nprint ahmed2\n\n\n&gt;&gt;&gt; False\n&lt;__main__.Person instance at 0xb74acf8c&gt;\n&lt;__main__.Person instance at 0xb74ac6cc&gt;\n&gt;&gt;&gt;\n\n</code></pre></li>\n<li><p>New-style classes</p>\n\n<pre><code>class Person(object):\n    _names_cache = {}\n    def __init__(self,name):\n        self.name = name\n    def __new__(cls,name):\n        return cls._names_cache.setdefault(name,object.__new__(cls,name))\n\nahmed1 = Person("Ahmed")\nahmed2 = Person("Ahmed")\nprint ahmed2 is ahmed1\nprint ahmed1\nprint ahmed2\n\n&gt;&gt;&gt; True\n&lt;__main__.Person object at 0xb74ac66c&gt;\n&lt;__main__.Person object at 0xb74ac66c&gt;\n&gt;&gt;&gt;\n</code></pre></li>\n</ol>\n'}, {'answer_id': 19950198, 'author': 'Ciro Santilli OurBigBook.com', 'author_id': 895245, 'author_profile': 'https://Stackoverflow.com/users/895245', 'pm_score': 8, 'selected': False, 'text': '<p><strong>Important behavior changes between old and new style classes</strong></p>\n\n<ul>\n<li><a href="https://stackoverflow.com/questions/576169/understanding-python-super-and-init-methods">super</a> added</li>\n<li>MRO changed (explained below)</li>\n<li><a href="http://docs.python.org/2/howto/descriptor.html" rel="noreferrer">descriptors</a> added</li>\n<li>new style class objects cannot be raised unless derived from <code>Exception</code> (example below)</li>\n<li><a href="http://docs.python.org/2/reference/datamodel.html#slots" rel="noreferrer"><code>__slots__</code></a> added</li>\n</ul>\n\n<h2>MRO (Method Resolution Order) changed</h2>\n\n<p>It was mentioned in other answers, but here goes a concrete example of the difference between classic MRO and C3 MRO (used in new style classes).</p>\n\n<p>The question is the order in which attributes (which include methods and member variables) are searched for in multiple inheritance.</p>\n\n<p><strong>Classic classes</strong> do a depth-first search from left to right. Stop on the first match. They do not have the <code>__mro__</code> attribute.</p>\n\n<pre><code>class C: i = 0\nclass C1(C): pass\nclass C2(C): i = 2\nclass C12(C1, C2): pass\nclass C21(C2, C1): pass\n\nassert C12().i == 0\nassert C21().i == 2\n\ntry:\n    C12.__mro__\nexcept AttributeError:\n    pass\nelse:\n    assert False\n</code></pre>\n\n<p><strong>New-style classes</strong> MRO is more complicated to synthesize in a single English sentence. It is explained in detail <a href="http://www.python.org/download/releases/2.3/mro/" rel="noreferrer">here</a>. One of its properties is that a base class is only searched for once all its derived classes have been. They have the <code>__mro__</code> attribute which shows the search order.</p>\n\n<pre><code>class C(object): i = 0\nclass C1(C): pass\nclass C2(C): i = 2\nclass C12(C1, C2): pass\nclass C21(C2, C1): pass\n\nassert C12().i == 2\nassert C21().i == 2\n\nassert C12.__mro__ == (C12, C1, C2, C, object)\nassert C21.__mro__ == (C21, C2, C1, C, object)\n</code></pre>\n\n<h2>New style class objects cannot be raised unless derived from <code>Exception</code></h2>\n\n<p>Around Python 2.5 many classes could be raised, and around Python 2.6 this was removed. On Python 2.7.3:</p>\n\n<pre><code># OK, old:\nclass Old: pass\ntry:\n    raise Old()\nexcept Old:\n    pass\nelse:\n    assert False\n\n# TypeError, new not derived from `Exception`.\nclass New(object): pass\ntry:\n    raise New()\nexcept TypeError:\n    pass\nelse:\n    assert False\n\n# OK, derived from `Exception`.\nclass New(Exception): pass\ntry:\n    raise New()\nexcept New:\n    pass\nelse:\n    assert False\n\n# `\'str\'` is a new style object, so you can\'t raise it:\ntry:\n    raise \'str\'\nexcept TypeError:\n    pass\nelse:\n    assert False\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/54867', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4883/']