FAQ: Learn Python: Inheritance and Polymorphism - Overriding Methods


#1

This community-built FAQ covers the “Overriding Methods” exercise from the lesson “Learn Python: Inheritance and Polymorphism”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Overriding Methods

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

Here’s a code to an exercise in the lesson:

class Message:
  def __init__(self, sender, recipient, text):
    self.sender = sender
    self.recipient = recipient
    self.text = text

class User:
  def __init__(self, username):
    self.username = username
    
  def edit_message(self, message, new_text):
    if message.sender == self.username:
      message.text = new_text

What I don’t get is how does Python know what message.sender refers to?? I thought in order to refer to the object, it would be self.sender. Similarly, I don’t know get what message.text refers to, and why is it not self.text.

Thanks.Preformatted text


#3

but message parameter of edit_message method is an instance of Message class. self within the User class refers to instance of User class.

so you need to instance of Message class to edit_message method

i recommend creating instances and calling the method(s), to see everything works. Let me know if you need more help.


#4

Thanks for your reply.

I think where I was confused was how did message in the edit_message method of the User class became an instance of the Message class. I thought to make an instance, you’d have to declare the instance explicitly. Is it the .sender part that makes message.sender attribute an instance of the Message class, or the message part of the message.sender that makes this attribute an instance of the Message class?

thanks again for your response.


#5

It doesn’t become a Message, that’s what the user of the method is supposed to send it, similar to how a and b do not become numbers in this function, that’s what the user is supposed to provide:

def add(a, b):
    return a + b

Sidenote: “declare” is a rarely used word in python, we skip that step altogether, we don’t declare the existence of things, we just put them there. For example, we don’t have undefined variables, because that would require declaring them (without defining them), which we don’t.
There’s an exception to this with function variables, those variables do get created before anything else happens:

def f():
    print(a)  # undefined, but does exist
    a = 5

# in contrast to:
print(a)  # doesn't exist at all
a = 5

#6

yes, currently messageof edit_message method is just a parameter. I thought i didn’t have to explain parameters vs arguments

when you call the method, you can pass an instance of Message class for the message parameter of edit_message method

that is why i recommend, you actual attempt to call the methods, to see how they work