Question for the example in 'Override!'


#1



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


For the example in 'Override!' How can we get the name of CEO or Employee by calling ' %other.name ' since we had never defined a variable called other.name, not to mention an assignment value


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!


#2

other in this implementation is a parameter that takes an instance, and applies the name attribute of that instance. ceo and emp are the instances.

emp.greet(ceo)
# Hello, Emily
ceo.greet(emp)
# Get back to work, Steve!

#3

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