Override!


#1



https://www.codecademy.com/courses/python-intermediate-en-WL8e4/2/3?curriculum_id=4f89dab3d788890003000096#


I'm having trouble understanding the example that was given. up until now, the method we defined inside the class took only one argument - "self". now it takes another one called "other". why does "other" have a "name" attribute? where did we define it? thanks in advance...


class Employee(object):
    def __init__(self, name):
        self.name = name
    def greet(self, other):
        print "Hello, %s" % other.name


#2

Remember, a function in a class can have any number of arguments but it must have a self argument.


#3

other will be a class instance, in this specific case ceo:

emp.greet(ceo)

ceo has a name attribute

the argument ceo now is stored in other (remember arguments and parameters?)


#4

ok, so If I understand correctly, if we didn't define an "other" inside the brackets, calling out:

emp.greet(ceo) will give an error? because it will only work in emp.greet()?


#5

great, thanks. I was thrown off by the exercise example as well.
I thought all the variable definitions are all made inside the init function or as member variables, but then hours was defined inside calculate_wage... I think I have it figured out now (the "Classes" Lesson made it a bit more clear ).


#6

yes, the amount of parameters must equal the numbers of arguments at function call


#7

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