Even after defining custom __str__ and __format__ methods they don't affect the
display of objects when they are in a list. Is there a way to change that,
other than explicitly converting each list element to a string?
The last line of output below shows that when I format the list I get standard
formatting of my objects instead of my custom format.
Code
#! /usr/bin/python3
from collections import namedtuple
class Foo(namedtuple("Foo", "x")):
__slots__ = ()
def __str__(self):
return "foolish({})".format(self.x)
def __format__(self, spec):
return self.__str__()
f=Foo(4)
print(f)
print(str(f))
print("{}".format(f))
print("{}".format([f])) # a list with one f
Output
foolish(4)
foolish(4)
foolish(4)
[Foo(x=4)]
I'm running Python 3.4.
Thanks.
Ross
--
https://mail.python.org/mailman/listinfo/python-list