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

ps9 prob2 I really dont understand what is going on with the __str__ function. Could someone take a look at this please. http://codepad.org/dVAhqwNp

OpenStudy (anonymous):

__str__ lets you define what happens when your object is the argument for print() and str(). if you define a __str__ function in your class - print(Class) will print the string you defined, similarly str(Class) will return the string you defined. there are other __xx__ functions you can define - i have seen them referred to as magic functions. we can build our own objects (Classes) and have them behave properly when we compare them by defining __eq__, __lt__, __gt__, __ge__, __le_. or what happens when you delete it - __del__ . here is a list of what is available http://docs.python.org/reference/datamodel.html#special-method-names after reading that it would be worthwhile going to the top of that section (3) and reading all of it (just read it through even if some of it doesn't make sense).

OpenStudy (anonymous):

thanks bw. I changed my for loop at the bottom to simply 'print ss' since ss is an instance of the class ShapeSet but that gives me another error: self.place+=1 unsupported operand for 'NoneType and 'int'

OpenStudy (anonymous):

you were close. the __iter__ function and __str__ functions are separate - you were trying to combine them. the __iter__ should return an iterator that 'generates' the shapes themselves http://docs.python.org/library/stdtypes.html#iterator-types ---> 5.5.1, then ---> http://docs.python.org/glossary.html#term-generator a generator expression looks just like a list comprehension except it is enclosed in parenthesis instead of brackets for ShapeSet.__str__ : just make a string that contains the str() representation of all the shapes i changed your functions if you want to look - http://codepad.org/QqsbNR6G

OpenStudy (anonymous):

Thanks for your changes. I ended up solving it with the __iter__ and next functions like in the handout. see attached. The problem I am having in this class is I just havent seen enough python code examples so I appreciate your imput.

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!