""" Short quiz 10 november. What is printed by the print calls below? Why? """ from numpy import * class MyList: def __init__(self,values): self.values = values def __add__(self,other): result = [] for i in range(len(self.values)): result.append(str(self.values[i])+'+' \ +str(other.values[i])) return result l1 = [2,3,4]; l2 = [5,6,1] a1 = array(l1); a2 = array(l2) m1 = MyList(l1); m2 = MyList(l2) print(l1+l2) print(a1+a2) print(m1+m2) """ The point of the quiz is to illustrate the use of special methods. The first print call prints the result of adding two list objects. The second adds two arrays, which have a different implementation of the addition operator. The third adds two objects of our own class, with a custom implementation of addition in the __add__ special method. The __add__ function is called whenever the + operator is used on our objects, but what __add__ actually does is completely up to us. """