This Looks Like a Job For...; question about super


#1

Hi. I completed the exercise, but have a question regarding the use of a function super()
Like in the example from the exercise below we write super(PartTimeEmployee, self)
What does the self in the brackets mean (do)? What self does it refer to? Could we omit it or write something else? What would happen then?

I tried reading Python documentation about it, but at my level it didn't really help.

Thanks in advance.

def full_time_wage(self, hours):
        return super(PartTimeEmployee, self).calculate_wage(hours)

#2

Super is a little advanced, and not every language uses it, but as you probably know, it's python's way of inheriting from classes. self is a little bit harder to explain, but it just refers to the object being created. When you are making arguments in a class, you always pass it self first. As far as i know, you can use any word to refer to the object being created, but self is just so overwhelmingly common that you can use it and other programmers have an easier time reading your code.
Python will use the first parameter that init() receives to refer to the object being created; this is why it's often called self, since this parameter gives the object being created its identity.


#3

Thank you for the reply.
I understand the super function and why do we use the self reference (at least in the scope of this lesson) I just don't get using it in that particular example:

super(PartTimeEmployee, self).calculatewage(hours)

PartTimeEmployee tells the super function to look for the method calculatewage in the parent class of PartTimeEmployee. But what does the self do in this case?


#4

I wish I could tell you that I understand, but I really don't, at least not in this case. I don't know why it comes second or exactly what it does. Obviously, it refers to the object being created, but I don't see where and I don't know much about it. You can always use stackoverflow and look for examples, just be sure to type in the language and the type of thing your referring to. I looked on there as well, and all I really found was this:

Sorry I couldn't be of more help! :slight_smile:


#5

No worries, thanks for the help. I was just curious, when I have time I'll do some searching on my own.


#6

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