Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary or useful to inherit from Python's object in Python 3.x?

In older Python versions when you create a class, it can inherit from object which is as far I understand a special built-in Python element that allows your class to be a new-style class.

What about newer versions (> 3.0 and 2.6)? I googled about the object class but I get so many results (for obvious reasons).

like image 427
thomas Avatar asked Aug 06 '09 12:08

thomas


People also ask

Do I need to inherit from object in Python?

In Python 2: always inherit from object explicitly. Get the perks. In Python 3: inherit from object 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.

Is inheritance useful in Python?

Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class.

What is inheritance most useful for in Python?

Inheritance enables us to define a class that takes all the functionality from a parent class and allows us to add more.

Does every class inherit from object Python?

All classes in Python are objects of the type class, and this type class is called Metaclass . Each class in Python, by default, inherits from the object base class.


2 Answers

You don't need to inherit from object to have new style in python 3. All classes are new-style.

like image 162
SilentGhost Avatar answered Sep 20 '22 18:09

SilentGhost


I realise that this is an old question, but it is worth noting that even in python 3 these two things are not quite the same thing.

If you explicitly inherit from object, what you are actually doing is inheriting from builtins.object regardless of what that points to at the time.

Therefore, I could have some (very wacky) module which overrides object for some reason. We'll call this first module "newobj.py":

import builtins  old_object = builtins.object  # otherwise cyclic dependencies  class new_object(old_object):      def __init__(self, *args, **kwargs):         super(new_object, self).__init__(*args, **kwargs)         self.greeting = "Hello World!"   builtins.object = new_object  #overrides the default object 

Then in some other file ("klasses.py"):

class Greeter(object):     pass  class NonGreeter:     pass 

Then in a third file (which we can actually run):

import newobj, klasses  # This order matters!  greeter = klasses.Greeter() print(greeter.greeting)  # prints the greeting in the new __init__  non_greeter = klasses.NonGreeter() print(non_greeter.greeting) # throws an attribute error 

So you can see that, in the case where it is explicitly inheriting from object, we get a different behaviour than where you allow the implicit inheritance.

like image 39
Philip Adler Avatar answered Sep 19 '22 18:09

Philip Adler