Class syntax confusion


#1

In 14. This Looks Like a Job For..., the prompt claims the syntax for a derived class is as follows:

class Derived(Base):
   def m(self):
       return super(Derived, self).m()

where .m() is the method from the base class. However, m is the derived method. So, the correct code for this sections looks like this:

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

My question is, why does code academy use m as both a placeholder for the derived method AND for the base method? This initially led me to believe the m from .m() and m(self) were supposed to be the same thing. Is there a relationship between the two that I am missing?


#3

They have the same method name in the derived and base class so that the derived method will by default override the base method. When the derived class needs to override its own method, it can reach up to the super method by the above syntax.

This call will pass the derived class instance to the base class to give its context to the method.


#4

It was my understanding that the derived method is full_time_wage while the base method is calculate_wage, meaning they don’t have the same method name?


#5

Must have my wires crossed. Can you please post a link to the exercise so I’m not working blind? Thanks.


#6

Found using google search
python super within def explain site:stackoverflow.com
https://stackoverflow.com/questions/222877/what-does-super-do-in-python?noredirect=1&lq=148

You could also search in the =Book=
super() site:python.org


#7

You could also have a look at the diagrams…
This Looks Like a Job For


#8

Both super and subclass have calculate_wage methods. That’s what I was referring to. It stems from this lesson…

https://www.codecademy.com/en/courses/python-intermediate-en-WL8e4/2/3

In the next lesson, the one I presume you are on,

https://www.codecademy.com/en/courses/python-intermediate-en-WL8e4/2/4

we see where a full_time-wage method is created in the derived class. This method reaches up the the calculate_wage method of the super. self in the parent class becomes the instance of the PartTime_employee class as though it is an instance of the Employee class.


#9

Perhaps I’ve done a poor job of explaining my confusion. In the prompt for the exercise I am on, which is 14. This Looks Like a Job For…, it shows that the syntax for defining a derived method using a placeholder m.

class Derived(Base):
   def m(self):
       return super(Derived, self).m()

The first m becomes full_time_wage, obviously because we’re defining the new, derived method. However, using m as a placeholder a second time [ .m() ] led me to believe I was supposed to use full_time_wage once more (because they are both m, so one would assume they represent the same value).

I quickly realized this was not the case, however I was confused as to why CodeAcademy would write the prompt in that way, and that perhaps it was actually intentional and I was missing something.


#10

I’d be willing to bet that you are not alone. One would have to put themself in the head of the author to know the reason why. Glad that you came to grips with this.


#11

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