How to make print call the __str__ method of Python objects inside a list?
python
Solution
Calling string on a python list calls the `__repr__` method on each element inside. For some items, `__str__` and `__repr__` are the same. If you want that behavior, do:
def __str__(self):
...
def __repr__(self):
return self.__str__()
Problem
In Java, if I call `List.toString()`, it will automatically call the `toString()` method on each object inside the List. For example, if my list contains objects `o1`, `o2`, and `o3`, `list.toString()` would look something like this: ``` "[" + o1.toString() + ", " + o2.toString() + ", " + o3.toString() + "]" ``` Is there a way to get similar behavior in Python? I implemented a `__str__()` method in my class, but when I print out a list of objects, using: ``` print 'my list is %s'%(list) ``` it looks something like this: ``` [<__main__.cell instance at 0x2a955e95f0>, <__main__.cell instance at 0x2a955e9638>, <__main__.cell instance at 0x2a955e9680>] ``` how can I get python to call my `__str__()` automatically for each element inside the list (or dict for that matter)?