FAQ: Learn Python: Inheritance and Polymorphism - Dunder Methods

salt is a Molecule instance which .atoms attribute is a list of Atom instances. It’s necessary to iterate the list so that we can extract each object’s .label attribute. The __repr__() method helps us create the final representation.

1 Like

thanks, I’ll try and see what happens

Hi all, would like some help to understand why my code is rejected by the exercise. I realise my code is less succinct but am not sure why it is wrong. The question and my code are below. Help is much appreciated

In script.py there are two classes defined, Atom and Molecule.

Give Atom a .__add__(self, other) method that returns a Molecule with the two Atoms together in a list.

My code

class Atom:
  def __init__(self, label):
    self.label = label
  def __add__(self, other):
    first_atom = [self.label]
    second_atom = [other.label]
    molecule_list = first_atom + second_atom
    return Molecule(self, molecule_list)

    
class Molecule:
  def __init__(self, atoms):
    if type(atoms) is list:
	    self.atoms = atoms
  
      
sodium = Atom("Na")
chlorine = Atom("Cl")
salt = Molecule([sodium, chlorine])
# salt = sodium + chlorine

Solution

class Atom:
  def __init__(self, label):
    self.label = label
    
  def __add__(self, other):
    return Molecule([self, other])
    
class Molecule:
  def __init__(self, atoms):
    if type(atoms) is list:
	    self.atoms = atoms
      
sodium = Atom("Na")
chlorine = Atom("Cl")
salt = Molecule([sodium, chlorine])
# salt = sodium + chlorine

That is not using the dunder method (__add__). It gets called when we use the plus sign in,

salt = sodium + chlorine

We don’t combine them in the Atom class. Your __add__() method never runs.

“that’s only scratching the tip of the iceberg.”

Best mixed metaphor I’ve heard in a long time!!

Is anyone has the answer for “Can you explain to me what do these variables other.red, other.blue and other.green do in the method? These variables are not declared before in the method so there should be a bug happen, right?” this question? I just can’t get it. :sob:

Those appear to be attributes, not just variables. An instance can have only one owner, and the owner accesses attributes and methods by way of self. In this case there are two class instances interacting. They can’t both be self when accessing own attributes, so one is self and the other is, well, other.

1 Like

Really appreciate your answer, I can’t go through this without your help!

3 Likes

I’ve been struggling with the Class topic and I think the Dunder methods example is where my head eventually crushed. I’m really curious why the exercise creators didn’t use a bit simpler example for the exercise? Why all the fuss with two classes when we could use only one - Atom - and focus on the dunder method itself?

class Atom:
  def __init__(self, label):
    self.label = label
  def __add__(self, other):
    return ''.join(x.label for x in [self, other])

sodium = Atom("Na")
chlorine = Atom("Cl")
salt = sodium + chlorine
print(salt)

You are so interesting! I don’t believe I’ve truly read through anything like that before. So wonderful to discover another person with a few unique thoughts on this issue. Seriously… many thanks for starting this up. This website is something that is required on the internet, someone with a bit of originality!

I really like reading through a post that can make people think. Also, many thanks for permitting me to comment!

what’s the purpose of calling min here? If we are passing say a new_red in’s that just one numeral value? pls help me understand this code, explain it in mundane terms if possible

class Color: 
  def __add__(self, other):
    """
    Adds two RGB colors together
    Maximum value is 255
    """
    new_red = min(self.red + other.red, 255)
    new_green = min(self.green + other.green, 255)
    new_blue = min(self.blue + other.blue, 255)
 
    return Color(new_red, new_green, new_blue)
1 Like

That is a way of guaranteeing the maximum value never exceeds 255. min() will return the smaller of the two values.

2 Likes

Hello mates,

I was enrolled in the Computer Science Path and almost finished the Python course, more exactly I was studying under Object Oriented Programming chapter “Inheritance and Polymorphism - Dunder Methods”, but suddenly this week I couldn’t find anymore this topic, is there any change in syllabus of this course?