This Looks Like a Job For


#1



class PartTimeEmployee(Employee):
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12.00
    def full_time_wage(self, hours):
        return super(PartTimeEmployee, self).calculate_wage(hours)

Can someone explain why do i need the "self" arg. to call the "super" function?
I am a little lost here hahaha
Further more, is it something about the init method?

what would happen if my code was like this:

class PartTimeEmployee(Employee):
    def __init__(self, name):
        #some code here bla bla bla
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12.00
    def full_time_wage(self, hours):
        return super(PartTimeEmployee, self).calculate_wage(hours)

Sorry for the silly question i think. :slight_smile:


#2

This expression within the code ...

super(PartTimeEmployee, self)

... creates a proxy that represents the self object in order to delegate method calls to the parent class of the PartTimeEmployee class. The self argument needs to be there to specify which object the created proxy object will represent.

See Python: super.


#3

Ok, to be super(self, clear)... [see what i did here? hahaha]

class B(A):
    def something(self, arg):
        super(B, self).somemethod(arg)

I'm actually passing self for/of the "Class A"?


#4

Hi, @mattaveleira ,

(Edited December 15, 2016 to add code to the example below)

Following is your most recent example, with code added for the class named A, so that it can execute, as well as a somemethod method of class B ...

class A(object):
    def somemethod(self, arg):
        print str(arg)
class B(A):
    def something(self, arg):
        super(B, self).somemethod(arg)
    def somemethod(self, arg):
        print(str(arg) + " and Goodbye")

thing = B()

thing.something("Hello")

In this something method, the self parameter represents whatever object from which the something method gets called ...

def something(self, arg):
    super(B, self).somemethod(arg)

Therefore, that object is what the proxy created by super will represent.

Here, we create thing as an instance of B ...

thing = B()

Now, we call the something method from that object ...

thing.something("Hello")

Because of the above call, the self parameter of something represents thing, and gets passed to super. As a result, thing acts as if it were an instance of A, via the proxy that super created, enabling the call to the somemethod method of class A.

Output:

Hello

#5

This clarifies everything :smiley: :smiley:

Thank you Appylpye, you are awesome. :slight_smile:


#6

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