What course/lesson teaches you about super()?

I’m a baby, just very very new to coding in general, and just finished cs101. I was very intrigued by classes and object-oriented programming(OOP), so when I went to see some of the other codes that involved what I’ve learned(classes and using methods) I noticed I was seeing a lot of 'super()". Began googling about it, and I got the part where it gives access to methods, but I really thought I could learn more about how to use them, and how they work. It was dead hard to understand them reading just from google and I figured Codeacademy is a great place to easily learn with these DIY lessons. So, I was wondering if there is any specific lesson or course that would teach me about this super() along with its concepts?

Please tell us what language you are familiar with. .super() is common to more than one language, such as JavaScript and Python.

Oh, I forgot to include it. I’m learning Python.
super() from python, like I saw them most getting used like this: super().init(whatever)

Say we have a class with three attributes:

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

class Member(Person):
    def __init__(self, name, gender, age, joindate):
        super().__init__(name, gender, age)
        self.joindate = joindate

Notice that Member inherits from Person? Also that there is overlap between three of the four parameters in Member? super() lets us instantiate the Person class on our Member instance.

What I’ve left off are methods. Both classes can have methods that access the attributes that are shared, but only the Member methods can access the joindate attribute.

Thanks for a quick sum-up! I now get super() acts as a one-way connection from the parent class to the child class when sharing attributes and I can build methods with them onward. But when it comes to enabling a child class using the parent class’s existing method, do I make that work by using super() every time for all the methods I want to use from the parent class? Something that would look like:

class Member(Person):
    def __init__(self, name, gender, age, joindate):
        super.__init__(self, name, gender, age)
        super.is_kind()
        super.healthy()
        self.joindate = joindate

or those inherit itself naturally enables the whole parent class’s method to be shared with the child class…?

Also, do you know whether there are practices available in Codeacademy? thanks :slight_smile:

I messed up, above, but went back just now to fix it. super(), with parens, and no self in the args.

As I understand it, we only invoke super when instantiating a new instance of a child class. It happens before we declare the attributes of the child.

Our instance will have access to all methods of both classes without involving super again.

In our above example, an instance declared as a Member, will also be an instance of Person. Will need to sit down with the code for moment; will be back, presently.

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

class Member(Person):
    def __init__(self, name, gender, age, joindate):
        super().__init__(name, gender, age)
        self.joindate = joindate
>>> weegillis = Member("Wee Gillis", "M", 19, "2023-02-23")
>>> isinstance(weegillis, Person)
True
>>> 

Now let’s take it a little further to where super helps access, rather than override, parent methods.

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

    def __repr__(self):
        return f"{self.name}\n{self.gender}\n{self.age}"

class Member(Person):
    def __init__(self, name, gender, age, joindate):
        super().__init__(name, gender, age)
        self.joindate = joindate

    def __repr__(self):
        s = super().__repr__()
        return f"{s}\n{self.joindate}"
>>> weegillis = Member("Wee Gillis", "M", 19, "2023-02-23")
>>> print (weegillis)
Wee Gillis
M
19
2023-02-23
>>> 

This allows our Person instance to remain anonymous, as that class is concerned. The identified Member instance in the above example can only access the Member method. super() lets that method access its counterpart in the parent class.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.