Thank you; I can confirm that overriding __repr__ makes the list display as I wanted.
The decision to use repr inside the list seems very odd, given the context, namely formatting something for display or looking for a simple string representation. It seems more natural to me to use str or, if in a format, the default formatting all the way down. Is there a good reason it's repr? Ross ________________________________________ From: Python-list [python-list-bounces+ross.boylan=ucsf....@python.org] on behalf of Chris Angelico [ros...@gmail.com] Sent: Thursday, November 17, 2016 3:24 PM To: python-list@python.org Subject: Re: how to control formatting of a namedtuple in a list On Fri, Nov 18, 2016 at 10:04 AM, Boylan, Ross <ross.boy...@ucsf.edu> wrote: > 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? > Yep! Inside a list, it's the repr that gets shown. So you should be able to do this: class Foo(namedtuple("Foo", "x")): def __repr__(self): return "foolish({})".format(self.x) This will also affect the other forms - if you don't define __str__, it'll use __repr__. So this should be all you need. ChrisA -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list