I can't seem to understand the logic of 'other.name' on the 5th line


#1



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")
emp.greet(ceo)
# Hello, Emily
ceo.greet(emp)
# Get back to work, Steve!

Why not use self.other?


#2

Hi @bytecoder18943 ,

In the method header below, other is placed within the parameter list in order to represent an object that is not the instance on which the method is to be called. Specifically, it is there to correspond to an argument, when the method is called, that represents another object, which could be either an Employee or a CEO instance.

Here the emp instance is asked to greet the ceo instance ...

emp.greet(ceo)

When the above call is made, other will represent ceo.

Analogously, here the ceo instance is asked to greet the emp instance ...

ceo.greet(emp)

When the above call is made, other will represent emp.

EDITED (August 17, 2016) to add the following:

Looking at the greet method of Employee ...

    def greet(self, other):
        print "Hello, %s" % other.name

... we want the current instance of Employee to state, within the printed greeting, the value of name that is associated with the other object, not the self object. That is why we use other.name.


#3

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