You don't need to inherit from object to have new style in python 3. All classes are new-style.
Is it necessary or useful to inherit from Python's object in Python 3.x? - Stack Overflow
Python inner classes inheritance from parent class
A question about super() and multiple inheritance...
How does inheritance of class variables work in Python? - Stack Overflow
Videos
You don't need to inherit from object to have new style in python 3. All classes are new-style.
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.
If I run the following code I get an error.
class A:
"""
This is the object which the other two objects
will inherit from.
"""
def __init__(self, a): print(a)
class B(A):
"""
This is one of the parent objects.
"""
def __init__(self, a, b):
super().__init__(a)
print(b)
class C(A):
"""
And the other one...
"""
def __init__(self, a, c):
super().__init__(a)
print(c)
class D(B, C):
"""
And here's the problem:
"""
def __init__(self, a, b, c, d):
B.__init__(self, a, b)
C.__init__(self, a, c)
print(d)
D('a', 'b', 'c', 'd')
The problem with this code is that I have an object which inherits from two different objects that both use the super() method in their constructors. From the object D, I'm calling the constructors of the objects B and C. The problem is that I'm calling them using the parent classes' identifiers and passing the child object as the self argument. When the class B calls super().__init__, the interpreter understands that it's being called from the object D. The interpreter does its best and calls the constructor for C, but the constructor of B is calling the constructor of C via super() only with the parameter a, so a positional argument gets missing and I get this error:
Traceback (most recent call last):
File ".../test.py", line 39, in <module>
D('a', 'b', 'c', 'd')
File ".../test.py", line 33, in __init__
B.__init__(self, a, b)
File ".../test.py", line 15, in __init__
super().__init__(a)
TypeError: __init__() missing 1 required positional argument: 'c'
Does anyone know if I can choose which constructor to call using the super() function? Or if anyone has another better idea, I'd be thankful (because my code is quite a mess...).
Any property of Dog will override a property inherited from Cat. You can re-define a value in Cat, but it won't matter because it has already been overridden by the child. For example:
class Cat:
age = 0 # Cat.age = 0
class Dog(Cat):
pass # Dog.age = Cat.age = 0
Dog.age=1 # Dog.age = 1, and Dog.age no longer points to Cat.age
Cat.age=2 # Cat.age = 2
print(Dog.age, Cat.age) # Dog.age is no longer Cat.age. They are completely different
Contrast that with this:
class Cat:
age = 0 # Cat.age = 0
class Dog(Cat):
pass # Dog.age = Cat.age = 0
Cat.age = 10 # Cat.age = 10
print(Dog.age, Cat.age) # Dog.age points to Cat.age, so Dog.age resolves to 10
Inheritance refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class. The class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class(es), and a method can call the method of a base class with the same name.
class Cat:
def __init__(self):
self.age = 2
self.sound = "meow"
class Dog(Cat):
def __init__(self):
super().__init__()
self.sound = "bark"
cat = Cat()
dog = Dog()
print(f"The cat's age is {cat.age}, and the dog's age is {dog.age}.")
print(f"Cats {cat.sound}, and dogs {dog.sound}.")
The cat's age is 2, and the dog's age is 2.
Cats meow, and dogs bark.
So, you can see the dog.age can inherit from class Cat. Notice the sound part, the method in the derived class overrides that in the base class. This is to say, we wrote the dog sound, it gets preference over the class Cat sound.