13 Override - understanding


#1



Hi,
I have a problem with understanding the usage of "other" argument in the "greet" method.
I mean, I get it it's just an argument's name, but how does the python know what is the "other.name" as it is not defined anywhere in the method.
I'm confused because in normal function it would be enough just to call an argument "other", without any additional things, like ".name" in this particular case, to have the argument substituted with a value ("Emily").
When I tried to run it without ".name" it returned "Hello, CEO object at 0x7efdf8800f90 ".
Is it because "name" is a member variable and to read it I need to address it? but why?

Maybe the answer is "it is just the way it is" but I would really prefer to have a sensible answer to this:)

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

The parameter defines it

f has a parameter, x. x gets assigned to the first argument, in the case of the invocation at the last line that argument is 5

def f(x):
    return x + 2

f(5)

If you want to read some value then yes, you'll have to say which one. Doing otherwise is a bit like expecting to print without saying what to print, but get what you want, right?
And if you print a CEO object, you'd expect a string representation of that object, not of one of its properties. You can override the __str__ method to redefine the string representation for CEO objects though


#3

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