4

I met this question as I learned Python on Codecademy, code as below:

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)
ceo.greet(emp)

I was wondering what does other.name mean here?

The self.name = name could be interpreted as a instance object's member variable self.name be set equal to name, so we can say that self is a instance and name is its property, right?

And, isn't the "Emily" assigned to the parameter other by ceo = CEO("Emily") and the "Steve" assigned to the name by emp = Employee("Steve")? How could it be used like that?

3
  • 2
    It is the name property of the "other" object, which is a parameter to that method. Commented May 2, 2015 at 13:15
  • 5
    other.name means "the .name property of the other object". other, which you passed emp to, is the employee object with the .name property "steve" Commented May 2, 2015 at 13:15
  • 2
    Note that e.g. ceo.greet(emp) resolves to CEO.greet(ceo, emp) - ceo is self, emp is other. Commented May 2, 2015 at 13:27

1 Answer 1

2

Class Attributes

other.name is the name attribute of any class instance that is passed as an argument to the other parameter of the greet() method.

Extended Example

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")

print emp.name, 'greets', ceo.name
emp.greet(ceo)
print
print ceo.name, 'greets', emp.name
ceo.greet(emp)

Steve greets Emily
Hello, Emily

Emily greets Steve
Get back to work, Steve!

Further inheritance explanation (edit)

CEO inherits everything defined by Employee (like the name attribute), but can alter things (like altering the greet() method).

Here is what is going on:

  1. "Emily" is assign to the name attribute of the CEO class when an instance of that class is created as ceo.

    ceo = CEO("Emily")
    
  2. "Steve" is assigned to the name attribute of the Employee class when an instance of that class is created as emp.

    emp = Employee("Steve")
    
  3. In the an instance's greet() call, the whole other class instance is passed to it thru the other parameter of that method.

    emp.greet(ceo)
    

    This passes all of ceo into emp.greet(), so that emp.greet() can access something from ceo, in this case, the name of ceo.

  4. The greet() call is repeated for the ceo instance

    ceo.greets(emp)
    

I hope this is clear, read about and play with some other examples of classes.


: this is what the __init__() method is for. __init__() can take any argument, assign anything, or run any code. It is called whenever an instance of the class is created.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! But isn't the "Emily" assigned to the parameter other by ceo = CEO("Emily") and the "Steve" assigned to the name by emp = Employee("Steve")?
@markcodd This is the core of inheritance, see updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.