-1

Trying to understand super() I made these two examples but they return the same results.

This is with super()

class Person1():
    def __init__(self, name):
        self.name = name

class EmailPerson1(Person1):
    def __init__(self, name, email):
        super().__init__(name)
        self.email = email       

bob2 = Person('Dim')
bob = EmailPerson1('Bob Frapples', 'bob@frapples.com')
bob.name

'Bob Frapples'

and this without super()

class Person():
    def __init__(self, name):
        self.name = name

class EmailPerson(Person):
    def __init__(self, name, email):
        self.name = name
        self.email = email       

bob2 = Person('Dim')
bob1 = EmailPerson('Bob Frapples', 'bob@frapples.com')
bob1.name

'Bob Frapples'

What is the difference? The first version should use the name from the parent class I think.

  • 2
    `bob2` is a different Bob to `bob1` – Sayse Dec 06 '19 at 14:35
  • Does this answer your question? [Python inheritance: when and why \_\_init\_\_](https://stackoverflow.com/questions/43624913/python-inheritance-when-and-why-init) – Sayse Dec 06 '19 at 14:37
  • Unfortunately the question in the link was not much help. If you do the relevant changes in my code so it is apparent what is the use of super() maybe it would be clear. – user12436030 Dec 06 '19 at 14:56
  • I'm not sure what changes you need to make, your question seems to be based around an incorrect premise that your two bobs are the same person which they aren't – Sayse Dec 06 '19 at 15:15

1 Answers1

0

The difference is you are manually duplicating the work done by Person.__init__ in EmailPerson.__init__, instead of using super().__init__ to make sure the inherited initializer does what it needs to.

class Person():
    def __init__(self, name):
        self.name = name

class EmailPerson(Person):
    def __init__(self, name, email):
        self.name = name
        self.email = email

Using super, i.e.,

class EmailPerson(Person):
    def __init__(self, name, email):
        super().__init__(name)
        self.email = email

means that if someone updated the definition of Person to do some additional work in Person.__init__:

class Person:
    def __init__(self, name):
        self.name = name.title()  # bob grapples -> Bob Frapples

you don't need to update the definition of EmailPerson at all to take advantage of the new change.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • I don't understand very well. How can the code I wrote as it is can prove the difference of super() being present or not.Thanks – user12436030 Dec 06 '19 at 14:58
  • I don't understand your question. With `super()`, you call `Person.__init__` and let it set the `name` attribute. Without it, you are forced to set it in `EmailPerson`, meaning you are writing the same bit of code twice. Since an instance of `EmailPerson` is also an instance of `Person`, you want to make sure anything `Person.__init__` does is also done for an instance of `EmailPerson`. Using `super` means that anything you change in `Person` automatically applies to `EmailPerson` as well. – chepner Dec 06 '19 at 15:01
  • It would be better to watch this in code. For example I tried to print some things but no matter if I use super or not the parent class seems irrelevant. Can you portray what you mean with an example based on the code I provided? I really don't get it. – user12436030 Dec 06 '19 at 15:06
  • 1
    It depends entirely on how you plan to use the data. Right now, `EmailPerson` models something that is identical to a `Person`, but also has an additional bit of data (the e-mail address) attached to an instance. Any function that expects an instance of `Person` can accept an instance of `EmailPerson`. Using `super` is just a tool to make sure that the creation of an `EmailPerson` instance stays consistent with your definition of `Person`. – chepner Dec 06 '19 at 15:13
  • Tried to recreate it but I don't know how to test it.Please add some prints with data for clarification. – user12436030 Dec 06 '19 at 15:22
  • I have no idea what you need to be clarified. – chepner Dec 06 '19 at 15:29
  • Tell me If I got it correctly: using super() we don't have to write in the child class about the parameter that is included in the super() (this can be done for many parameters from the parent class at the same time by adding them in the parenthesis). Also by using super() any updates that happen in the specific parameter are done at the same time to the child class. – user12436030 Dec 06 '19 at 15:29
  • I get the feeling that you don't quite understand inheritance. Would you still be confused if I said to use `Person.__init__(self, name)` instead of `super().__init__(name)`? – chepner Dec 06 '19 at 15:32
  • Is my explanation accurate in the last comment? – user12436030 Dec 06 '19 at 15:49