11/11 Code that works


This works... apparently.

I defined repr originally with no input arguments, adding self as an argument makes the code work. On a side note, I really do feel that this and the last lesson on classes was very unclear, and many things were left fully unexplained. For example, why even use repr at all.

class Point3D(object):
def init(self, x, y, z):
self.x = x
self.y = y
self.z = z
def repr(self):
return "(%d, %d, %d)" % (self.x, self.y, self.z)

my_point = Point3D(1, 2, 3)
print my_point


@kenrutan: As you stated, using self as an argument for __repr__() is essential but the reason why we redefine the __repr__ method should come clear to you if you omit it or comment it out and try to print the object... :smile:

As the Python Documentation for __repr__() tells us:

...[it is used] to compute the “official” string representation of an object...

so trying to

print my_point

without it won't yield the coordinates of your point but the object adress instead:

<Point3D object at 0x7f289768a310>

I'll give it to you, we could have used __str__() as well to achieve a similar result.