I have a list of objects. I am trying to output a meaningful representation of the objects in the list, that reveals some of the instance variables, using the string formatting capability built into recent versions of Python. I am using Python 3.3.0.
I have defined a __str__ method in the class for the objects in my list. If I try to print an individual object, I get the representation returned by the __str__ method. But if I try to print the whole list, I get the generic representation, as if I haven't defined the __str__ method. Here is an example:
class CustomClass:
    def __init__(self):
        self.instanceVar = 42
    def __str__(self):
        return "{} (instanceVar = {})".format(self.__class__.__name__, self.instanceVar)
testObject = CustomClass()
print("{}".format(testObject))
# CustomClass (instanceVar = 42)
print("{}".format([testObject]))
# [<__main__.CustomClass object at 0x1006cbed0>]
How can I get the 2nd print statement to print something like [CustomClass (instanceVar = 42)]? Is it possible to do this just by passing the right format string to the print function or is it more complicated than that?
 
     
    