Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

Hi guys, I'm working on problem set 9, question 2. In the very last part of the question we are supposed to implement __str__ for the ShapeSet class, which prints the string representation of each object in the ShapeSet class. I can't figure out how to do this correctly. Here is my code: class ShapeSet: def __init__(self): self.shapes=[] #Skipping some irrelevant functions... def __str__(self): for shape in self.shapes: print shape When I do the same thing in the main program (use a for loop to print each object in the ShapeSet class

OpenStudy (anonymous):

sorry ran out of room... it works correctly, but when I do it within the ShapeSet class I get a TypeError: __str__ returned non-string (type NoneType) . I'm not sure what I am doing wrong...can anyone help?

OpenStudy (anonymous):

what you posted looks ok - use a code pasting site and post all your classes the error gives a clue it says that None was returned - what 'command' or statement produced that? try changing the for loop to return '\n'.join([str(thing) for thing in self.shapes]) then as long as shapes contains objects with a __str__ function, print shapeset should work those magic functions (__xxx__) should return something - not do something.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!