Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to control formatting of a namedtuple in a list

519 views
Skip to first unread message

Boylan, Ross

unread,
Nov 17, 2016, 6:19:17 PM11/17/16
to
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

Chris Angelico

unread,
Nov 17, 2016, 6:24:43 PM11/17/16
to
On Fri, Nov 18, 2016 at 10:04 AM, Boylan, Ross <Ross....@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

Boylan, Ross

unread,
Nov 17, 2016, 6:50:07 PM11/17/16
to
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: pytho...@python.org
Subject: Re: how to control formatting of a namedtuple in a list

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list

Ned Batchelder

unread,
Nov 17, 2016, 7:09:08 PM11/17/16
to
On Thursday, November 17, 2016 at 6:50:07 PM UTC-5, Boylan, Ross wrote:
> 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?


I think of it as str is for customers, repr is for developers. Or, repr is
for nerds, str is for civilians, etc. If you print a list, then you will
get square brackets and commas separating the display. That is, you are
already getting a nerdy output. So the contents of the list are also
displayed in the nerdy way, with repr.

If you want a nice display of the contents, you also have to control the
display of the list itself, so you can do whatever you need, and use str
on the contents yourself.

--Ned.

MRAB

unread,
Nov 17, 2016, 7:09:43 PM11/17/16
to
On 2016-11-17 23:49, Boylan, Ross wrote:
> 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

Given a string, say:

>>> s = 'foo'

str shows:

>>> print(str(s))

whereas repr shows:

>>> print(repr(s))
'foo'

If it was in a list, would you want it to show:

[foo]

or:

['foo']

?

Ethan Furman

unread,
Nov 17, 2016, 7:18:01 PM11/17/16
to
On 11/17/2016 04:09 PM, MRAB wrote:
> On 2016-11-17 23:49, Boylan, Ross wrote:

>> 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?
>
> Given a string, say:
>
> >>> s = 'foo'
>
> str shows:
>
> >>> print(str(s))
>
> whereas repr shows:
>
> >>> print(repr(s))
> 'foo'
>
> If it was in a list, would you want it to show:
>
> [foo]
>
> or:
>
> ['foo']
>
> ?

Another example:

>>> foo = 'ham, eggs, cheese'
>>> bar = 'bacon, toast'

if list used str instead of repr:

>>> print(list(foo, bar))
[ham, eegs, cheese, bacon, toast]

How many items are in that list? (Hint: it isn't 5. ;)

--
~Ethan~

Boylan, Ross

unread,
Nov 17, 2016, 8:40:39 PM11/17/16
to
Actually,
>> print(list(foo, bar))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (2 given)

Though
>>> [foo, bar]
['ham, eggs, cheese', 'bacon, toast']
which admittedly would be confusing if the strings weren't quoted. But display, or formatted values, are not intended to allow reconstruction of the objects; that's what repr is for.

The argument that the display of list is already low-level, and so everything inside the list should be displayed low-level, seems a bit of a stretch.

Overriding repr is serviceable for me, but it requires violating the intended semantics of repr, namely that the result could be converted back to the original object. I'm trying to get a display that has only some of the information in the object. My understanding is that str is supposed to provide that.

At any rate, I agree there are reasons to use repr inside a list.

Ross
________________________________________
From: Python-list [python-list-bounces+ross.boylan=ucsf...@python.org] on behalf of Ethan Furman [et...@stoneleaf.us]
Sent: Thursday, November 17, 2016 4:18 PM


To: pytho...@python.org
Subject: Re: how to control formatting of a namedtuple in a list

On 11/17/2016 04:09 PM, MRAB wrote:

Another example:

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list

0 new messages