FAQ: Object-Oriented Programming - Dunder Methods

This community-built FAQ covers the “Dunder Methods” exercise from the lesson “Object-Oriented Programming”.

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

Learn Intermediate Python 3

FAQs on the exercise Dunder Methods

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

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!

Could any one explain to me why this is correct:

m1 + e1
m1 + e2
m1 + e3

any this is not:

m1 = m1 + e1
m1 = m1 + e2
m1 = m1 + e3
1 Like

It all depends on how you define the __add__ method. Remember that for an example like m1 + e1 you can equate it to calling the .__add__ the method of the m1 object with the right-hand operand as the argument: m1.__add__(e1). So what does your method do when it is called?

Without a return all you have are side effects of mutating an existing attribute of m1. If you defined this method with a return of self or a similar meeting object then you could consider assigning the output.

In this example a better method to use might be += (associated with the __.iadd__ method) as it’s really an in-place alternation which is more consistent behaviour with other Python types.

Short version: the second does not work because .__add__ does not return anything so there’s nothing to assign.

4 Likes

Hi, i have another question.
i tried “m1+e1+e2+e3” but seems doesnt work, could you please give me a hint?

I have a question, under the class Meeting, inside the “def__add__(self, employee)” method we have written:
def add(self, employee):
print(“ID {} added.”.format(employee.id))

How can we access the “id” attribute that is inside the class Employee, the “id” attribute is encapsulated inside the Employee class…?

Below is the original code:
class Employee():
new_id = 1
def init(self):
self.id = Employee.new_id
Employee.new_id += 1

class Meeting:
def init(self):
self.attendees =

def add(self, employee):
print(“ID {} added.”.format(employee.id))
self.attendees.append(employee)

2 Likes

Good lesson all in all, but could someone tell me how to access the list of attendees created in class Meeting?

Been trying various ways but all i get back are:

[<__main__.Employee object at 0x7f50b3640128>, <__main__.Employee object at 0x7f50b36400f0>, <__main__.Employee object at 0x7f50b36400b8>]

Kindly
Andy

Hi all,

I hope you can clear me of a doubt.

I printed the m1.attendees list and as was expected [1, 2, 3] was printed. But first I wrote the reps method otherwise the output will be a list of Employee objects and their memory direction. So I just want to confirm if the m1.attendees list is conformed by Employee objects, but when printing the list the output call the repr method on Employee class?

class Employee():
  new_id = 1
  def __init__(self):
    self.id = Employee.new_id
    Employee.new_id += 1
  
  def __repr__(self):
    return str(self.id)

class Meeting:
  def __init__(self):
    self.attendees = []
  
  def __add__(self, employee):
    print("ID {} added.".format(employee.id))
    self.attendees.append(employee)
  
  def __len__(self):
    return len(self.attendees)

# Write your code
e1 = Employee()
e2 = Employee()
e3 = Employee()
m1 = Meeting()

m1 + e1
m1 + e2
m1 + e3

print(len(m1))
print(m1.attendees)
print(type(m1.attendees[0]))

And the output is like this:

ID 1 added.
ID 2 added.
ID 3 added.
3
[1, 2, 3]
<class '__main__.Employee'>

Hi, I was wondering how “case-sensitiveness” works in this exercise. To my knowledge Python is case-sensitive, but here class Employee and variable employee of add() method are used interchangibly. Has anyone got an insight on this?

class Employee():
  new_id = 1
  def __init__(self):
    self.id = Employee.new_id
    Employee.new_id += 1

class Meeting:
  def __init__(self):
    self.attendees = []
  
  def __add__(self, employee):
    print("ID {} added.".format(employee.id))
    self.attendees.append(employee)

  # Write your code
  def __len__(self):
    return len(self.attendees)
    
e1 = Employee()
e2 = Employee()
e3 = Employee()
m1 = Meeting()
m1 + e1
m1 + e2
m1 + e3
print(len(m1))
1 Like

How does python understands that m1 + e1 is equivalent to m1.add(e1)? In the Employee class id is not returned it is just defined

Hello, it is because you don’t return anything in the add method. So when you try m1 + e1 + e2, it says that you are trying to add NoneType and Employee. So what you need to do is return the meeting itself.

class Meeting:
    def __init__(self):
        self.attendees = []
    
    def __add__(self, employee):
        print("ID {} added.".format(employee.id))
        self.attendees.append(employee)
        return self
    
    def __len__(self):
        return len(self.attendees)

This way every time you add an employee like this m1 + e1 + e2 + e3 + e4 it will do the first operation first return the meeting and proceed to add the next