13/18 Override! example



This is the example code:
`class Employee(object):
def init(self, name):
self.name = name
def greet(self, other):
print "Hello, %s" % other.name

class CEO(Employee):
def greet(self, other):
print "Get back to work, %s!" % other.name

ceo = CEO("Emily")
emp = Employee("Steve")

Hello, Emily


Get back to work, Steve!`

class Employ is created and initializes, name defined.
then method GREET(self, other) is created: first question is what is this "other"?? just something irrelevant or does it actually get any value passed to it?

So, let's create and call Steve:
emp = Employee("Steve")
Second (and the most important to me) question is, why does it NOT print Hello Steve? The scope should be somewhere withing method GREET, class Emplyee class Employee, not in class CEO. However, when you do ceo.greet(emp), it magically jumps to class CEO(Employee) and prints "Get back to work, %s!".

Can someone explain this thoroughly step by step, please? I keep coming back to this over and over...


It is a parameter of the method, an instance object. It can be an instance of Employee, or of the sub-class, CEO. Notice that the method queries the name property of that instance?


(1) In short, the "other" is a simple argument.

You can pass anything or any name to it you wan't. Other is the argument for the methods greet. So when emp.greet(ceo) is called, the employee class is activated because we instantiated emp with the employee class(emp = Employee("Steve"). Then we use the method greet under the Employee class. The def greet(self, other) then calls for an object/variable (in this case Emp) and then takes 1 argument which is used in other.name. The argument is anything that is entered in it. This way it will accept ceo as argument and generate ceo.name ("Emily") or emp.name("Steve"). It is not an attribute like it would be in the init function. I hope this helps.

(2) ceo.greet(emp) does not print "Hello Steve" because it uses the method/function under the CEO class.

ceo is an object. You instantiated the object under the CEO class (ceo = CEO("Emily")). So, the attribute for variable emp is "Steve". "Steve" is passed to the agrument as other and "Get back to work Steve is printed" because we are using that method because ceo's class is CEO.