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


#7

Hi there, I’ve noticed that when I write “new_text = message.text” the code is not accepted. Whereas “message.text = new_text” is. Is this a bug, or is putting an argument on the left side of the equation and method on the right, considered incorrect syntax? Here’s what I mean:


Works


Doesn’t work


#8

if the syntax was incorrect, you would get a syntax error. The answer to your question can be found in understanding what self.text = new_text does.

when we create a new instance of a class:

admin = Admin("Top secret message")

we pass argument(s) to the parameter(s) of the class constructor. Then in the constructor we define instance variable (text), so we can access this instance variable in other methods:

class Admin(User):
  def edit_message(self, message, new_text):
    message.text = new_text
  def print_text(self):
     print(self.text)

admin = Admin("Top secret message")
admin.print_text()

#9

I understand that part, but what I mean was, does the instance variable always have to be on the left side of the equation in a case like this? Or is it just a bug in the CodeCademy learning environment?


#10

But if you understand that, the question doesn’t make much sense.

the variable you want to assign/give a value is always on the left side of the equal sign:

x = 5

On the right side of the equal sign is always the value you want to assign to the variable.

why would you want to assign an undefined instance variable to the init method parameter?


#11

Sorry, it’s the equals sign that was confusing me. I kept thinking if ‘a = b’ then ‘b = a’. I get what you mean now. Thanks.


#12

message is a parameter of edit_message method which belongs to User class. how come suddenly sender which belongs to Message class got related to message parameter out of no where?


#13

Following is some example code that instantiates the User and Message classes to demonstrate how this works:

monty = User("Monty")
eric = User("Eric")
# Create the Message instance
montyMessage = Message("Monty", "Eric", "I don't like Spam!")
print(montyMessage.text)
# Edit the Message instance
monty.edit_message(montyMessage, "I love Spam!")
print(montyMessage.text)

Notice that we have created an instance of Message and assigned it to montyMessage. That instance has a sender instance variable. When we call the edit_message method from monty, which is a User, we pass montyMessage to that method. Now the edit_message method can access the sender instance variable of montyMessage.

The output is:

I don't like Spam!
I love Spam!

#14

it makes so much sense now :grinning:…thanks a lot


#15
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
      
class Admin(User):
  def edit_message(self, message, new_text):
    if message.sender != self.username:
      try:
        message.text = new_text
      except User:
        print("Do something")
  

This worked, but does this make sense to anyone or am I overdoing my code?