Why do Python classes inherit object? - Stack Overflowmost recent 30 from stackoverflow.com2026-04-14T08:41:12Zhttps://stackoverflow.com/feeds/question/4015417https://creativecommons.org/licenses/by-sa/4.0/rdfhttps://stackoverflow.com/q/40154171717Why do Python classes inherit object?tjvrhttps://stackoverflow.com/users/3596402010-10-25T14:15:26Z2022-04-09T07:21:43Z
<p>Why does the following class declaration inherit from <code>object</code>?</p>
<pre><code>class MyClass(object):
...
</code></pre>
https://stackoverflow.com/questions/4015417/-/4015465#401546535Answer by knitti for Why do Python classes inherit object?knittihttps://stackoverflow.com/users/4505172010-10-25T14:20:34Z2018-02-15T14:06:28Z<p>Yes, it's <a href="http://www.python.org/doc/newstyle/" rel="noreferrer">historical</a>. Without it, it creates an old-style class.</p>
<p>If you use <code>type()</code> on an old-style object, you just get "instance". On a new-style object you get its class.</p>
https://stackoverflow.com/questions/4015417/-/4015466#4015466411Answer by Jerub for Why do Python classes inherit object?Jerubhttps://stackoverflow.com/users/146482010-10-25T14:20:56Z2017-10-27T18:54:12Z<p>Yes, this is a 'new style' object. It was a feature introduced in python2.2.</p>
<p>New style objects have a different object model to classic objects, and some things won't work properly with old style objects, for instance, <code>super()</code>, <code>@property</code> and descriptors. See <a href="http://docs.python.org/release/2.2.3/whatsnew/sect-rellinks.html" rel="noreferrer">this article</a> for a good description of what a new style class is.</p>
<p>SO link for a description of the differences: <a href="https://stackoverflow.com/questions/54867/old-style-and-new-style-classes-in-python">What is the difference between old style and new style classes in Python?</a></p>
https://stackoverflow.com/questions/4015417/-/9448136#9448136610Answer by Yarin for Why do Python classes inherit object?Yarinhttps://stackoverflow.com/users/1656732012-02-25T21:15:32Z2020-04-19T11:16:13Z<p><strong>Python 3</strong></p>
<ul>
<li><code>class MyClass(object):</code> = New-style class</li>
<li><code>class MyClass:</code> = New-style class (implicitly inherits from <code>object</code>)</li>
</ul>
<p><strong>Python 2</strong></p>
<ul>
<li><code>class MyClass(object):</code> = New-style class</li>
<li><code>class MyClass:</code> = <em>OLD-STYLE CLASS</em></li>
</ul>
<hr>
<p><strong>Explanation</strong>:</p>
<p>When defining base classes in Python 3.x, you’re allowed to drop the <code>object</code> from the definition. However, this can open the door for a seriously hard to track problem…</p>
<p>Python introduced new-style classes back in Python 2.2, and by now old-style classes are really quite old. Discussion of old-style classes is <a href="http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes" rel="noreferrer">buried in the 2.x docs</a>, and non-existent in the 3.x docs.</p>
<p>The problem is, <strong>the syntax for old-style classes in Python 2.x is the same as the alternative syntax for new-style classes in Python 3.x</strong>. Python 2.x is still very widely used (e.g. GAE, Web2Py), and any code (or coder) unwittingly bringing 3.x-style class definitions into 2.x code is going to end up with some seriously outdated base objects. And because old-style classes aren’t on anyone’s radar, they likely won’t know what hit them.</p>
<p>So just spell it out the long way and save some 2.x developer the tears.</p>
https://stackoverflow.com/questions/4015417/-/26733360#2673336040Answer by disp_name for Why do Python classes inherit object?disp_namehttps://stackoverflow.com/users/29601732014-11-04T10:54:50Z2017-12-17T08:59:43Z<p>History from <a href="http://learnpythonthehardway.org/book/ex42.html" rel="noreferrer">Learn Python the Hard Way</a>:</p>
<blockquote>
<p>Python's original rendition of a class was broken in many serious
ways. By the time this fault was recognized it was already too late,
and they had to support it. In order to fix the problem, they needed
some "new class" style so that the "old classes" would keep working
but you can use the new more correct version.</p>
<p>They decided that they would use a word "object", lowercased, to be
the "class" that you inherit from to make a class. It is confusing,
but a class inherits from the class named "object" to make a class but
it's not an object really its a class, but don't forget to inherit
from object.</p>
</blockquote>
<p>Also just to let you know what the difference between new-style classes and old-style classes is, it's that new-style classes always inherit from <code>object</code> class or from another class that inherited from <code>object</code>:</p>
<pre><code>class NewStyle(object):
pass
</code></pre>
<p>Another example is:</p>
<pre><code>class AnotherExampleOfNewStyle(NewStyle):
pass
</code></pre>
<p>While an old-style base class looks like this:</p>
<pre><code>class OldStyle():
pass
</code></pre>
<p>And an old-style child class looks like this:</p>
<pre><code>class OldStyleSubclass(OldStyle):
pass
</code></pre>
<p>You can see that an Old Style base class doesn't inherit from any other class, however, Old Style classes can, of course, inherit from one another. Inheriting from object guarantees that certain functionality is available in every Python class. New style classes were introduced in Python 2.2 </p>
https://stackoverflow.com/questions/4015417/-/40146254#4014625410Answer by kmario23 for Why do Python classes inherit object?kmario23https://stackoverflow.com/users/29560662016-10-20T05:37:38Z2016-10-20T05:37:38Z<p>The syntax of the class creation statement:</p>
<pre><code>class <ClassName>(superclass):
#code follows
</code></pre>
<p>In the absence of any other superclasses that you specifically want to inherit from, the <code>superclass</code> should always be <em><code>object</code></em>, which is the root of all classes in Python.</p>
<p><em><code>object</code></em> is technically the root of "new-style" classes in Python. But the new-style classes today are as good as being the only style of classes.</p>
<p>But, if you don't explicitly use the word <code>object</code> when creating classes, then as others mentioned, Python 3.x implicitly inherits from the <code>object</code> superclass. But I guess explicit is always better than implicit (hell)</p>
<p><a href="https://newcircle.com/bookshelf/python_fundamentals_tutorial/oop" rel="noreferrer">Reference</a></p>
https://stackoverflow.com/questions/4015417/-/45062077#450620771265Answer by Dimitris Fasarakis Hilliard for Why do Python classes inherit object?Dimitris Fasarakis Hilliardhttps://stackoverflow.com/users/49521302017-07-12T15:34:59Z2019-08-14T18:37:47Z<blockquote>
<h3>Is there any reason for a class declaration to inherit from <code>object</code>?</h3>
</blockquote>
<p>In Python 3, apart from compatibility between Python 2 and 3, <em>no reason</em>. In Python 2, <em>many reasons</em>. </p>
<hr>
<h3>Python 2.x story:</h3>
<p>In Python 2.x (from 2.2 onwards) there's two styles of classes depending on the presence or absence of <code>object</code> as a base-class:</p>
<ol>
<li><p><strong>"classic" style</strong> classes: they don't have <code>object</code> as a base class:</p>
<pre><code>>>> class ClassicSpam: # no base class
... pass
>>> ClassicSpam.__bases__
()
</code></pre></li>
<li><p><strong>"new" style</strong> classes: they have, directly <em>or indirectly</em> (e.g inherit from a <a href="https://docs.python.org/3/library/stdtypes.html" rel="noreferrer">built-in type</a>), <code>object</code> as a base class:</p>
<pre><code>>>> class NewSpam(object): # directly inherit from object
... pass
>>> NewSpam.__bases__
(<type 'object'>,)
>>> class IntSpam(int): # indirectly inherit from object...
... pass
>>> IntSpam.__bases__
(<type 'int'>,)
>>> IntSpam.__bases__[0].__bases__ # ... because int inherits from object
(<type 'object'>,)
</code></pre></li>
</ol>
<p>Without a doubt, when writing a class you'll <em>always</em> want to go for new-style classes. The perks of doing so are numerous, to list some of them:</p>
<ul>
<li><p><a href="https://docs.python.org/3/howto/descriptor.html" rel="noreferrer">Support for descriptors</a>. Specifically, the following constructs are made possible with descriptors: </p>
<ol>
<li><a href="https://docs.python.org/3/library/functions.html#classmethod" rel="noreferrer"><code>classmethod</code></a>: A method that receives the class as an implicit argument instead of the instance.</li>
<li><a href="https://docs.python.org/3/library/functions.html#staticmethod" rel="noreferrer"><code>staticmethod</code></a>: A method that does not receive the implicit argument <code>self</code> as a first argument.</li>
<li>properties with <a href="https://docs.python.org/3/library/functions.html#property" rel="noreferrer"><code>property</code></a>: Create functions for managing the getting, setting and deleting of an attribute. </li>
<li><a href="https://docs.python.org/3/reference/datamodel.html#slots" rel="noreferrer"><code>__slots__</code></a>: Saves memory consumptions of a class and also results in faster attribute access. Of course, it does <a href="https://docs.python.org/3/reference/datamodel.html#notes-on-using-slots" rel="noreferrer">impose limitations</a>.</li>
</ol></li>
<li><p>The <a href="https://docs.python.org/3/reference/datamodel.html#object.__new__" rel="noreferrer"><code>__new__</code></a> static method: lets you customize how new class instances are created. </p></li>
<li><p><a href="https://www.python.org/download/releases/2.3/mro/" rel="noreferrer">Method resolution order (MRO)</a>: in what order the base classes of a class will be searched when trying to resolve which method to call. </p></li>
<li><p>Related to MRO, <a href="https://docs.python.org/3/library/functions.html#super" rel="noreferrer"><code>super</code> calls</a>. Also see, <a href="https://rhettinger.wordpress.com/2011/05/26/super-considered-super/" rel="noreferrer"><code>super()</code> considered super.</a></p></li>
</ul>
<p>If you don't inherit from <code>object</code>, forget these. A more exhaustive description of the previous bullet points along with other perks of "new" style classes can be found <a href="https://www.python.org/download/releases/2.2.3/descrintro/" rel="noreferrer">here</a>.</p>
<p>One of the downsides of new-style classes is that the class itself is more memory demanding. Unless you're creating many class objects, though, I doubt this would be an issue and it's a negative sinking in a sea of positives.</p>
<hr>
<h3>Python 3.x story:</h3>
<p>In Python 3, things are simplified. Only new-style classes exist (referred to plainly as classes) so, the only difference in adding <code>object</code> is requiring you to type in 8 more characters. This:</p>
<pre><code>class ClassicSpam:
pass
</code></pre>
<p>is completely equivalent (apart from their name :-) to this:</p>
<pre><code>class NewSpam(object):
pass
</code></pre>
<p>and to this:</p>
<pre><code>class Spam():
pass
</code></pre>
<p>All have <code>object</code> in their <code>__bases__</code>.</p>
<pre><code>>>> [object in cls.__bases__ for cls in {Spam, NewSpam, ClassicSpam}]
[True, True, True]
</code></pre>
<hr>
<h2>So, what should you do?</h2>
<p><strong>In Python 2:</strong> <em>always inherit from <code>object</code> explicitly</em>. Get the perks.</p>
<p><strong>In Python 3:</strong> inherit from <code>object</code> if you are writing code that tries to be Python agnostic, that is, it needs to work both in Python 2 and in Python 3. Otherwise don't, it really makes no difference since Python inserts it for you behind the scenes.</p>