0

Being new to Python, I'm reading book on Object Oriented Python and came across the following code while the writer experiments with defining the PARENT class which will be inherited by subclasses.

class Property:
    def __init__(self, square_feet='', beds='', baths='', **kwargs):
        super().__init__(**kwargs)
        self.square_feet = square_feet
        self.num_bedrooms = beds
        self.num_baths = baths

I understand the call to super().__init__(**kwargs) is used when we are inheriting to a subclass. But I don't get why do we need to make this call when we are defining the parent class. Does it have to do that by default every class in Python is subclass of "Object" class?

The writer explains that:

We've also included a call to super().__init__ in case we are not the last call in the multiple inheritance chain. In this case, we're "consuming" the keyword arguments because we know they won't be needed at other levels of the inheritance hierarchy.

I totally don't get that why do we have to do this call at PARENT CLASS level when we are not inheriting?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Hannan
  • 1,171
  • 6
  • 20
  • 39
  • The author states, "not the last call in the multiple inheritance chain", I am interpreting this as meaning he is proposing that there is the possibility for this to be a child class of another, in which instance we want to pass `**kwargs` to this parent class as they will not be used in this class. – Professor_Joykill Jul 19 '17 at 19:07
  • 1
    Hmm, if this is Python 3.x, then there is an implicit base class (`Object`). But if it's 2.7, then the call is pointless here. And the Author is wrong as well in that they are consuming the named arguments, not the keyword ones – kdopen Jul 19 '17 at 19:13
  • Please see https://stackoverflow.com/a/27134600/4014959 and Python core dev Raymond Hettinger's article: [super considered super](https://rhettinger.wordpress.com/2011/05/26/super-considered-super/) – PM 2Ring Jul 19 '17 at 19:21
  • @kdopen It can't be Python 2, since it uses the arg-less form of `super`, which doesn't exist in Python 2. – PM 2Ring Jul 19 '17 at 19:29

0 Answers0