Does __add__ for a class have to return a different class?


#1

Question

In this exercise, the __add__() method is implemented for the Atom class but returns a Molecule class. Does __add__() always need to return a different class?

Answer

NO. The __add__() method can be implemented to return whatever type or class makes sense for the operation. For this exercise, the addition of two Atom objects returns a Molecule. In some other case, a class may just return a new instance of the same class. The __add__() method is free to return whatever makes sense for the operation being performed.