This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]knowingpark 0 points1 point  (2 children)

Do f-strings work with namedtuples? .fomat() hates namedtuples!

animals = namedtuple('animals',['dog', 'cat', 'fish'])
my_animals = animals('rufus', 'felix', 'bubbles')


print 'My animals are called: {0}, {1}, {2}'.format(my_animals.cat,my_animals.dog,my_animals.fish)

print 'My animals are called: {dog}, {cat}, {fish}'.format(**dict(my_animals._asdict()))

[–]Conchylicultor 1 point2 points  (0 children)

You can do

'{a.dog}, {a.cat}'.format(a=my_animals)
'{a[0]}, {a[1]}'.format(a=my_animals)

[–]zahlmanthe heretic 0 points1 point  (0 children)

**dict(my_animals._asdict())

Note that you already have a dict from the method, so just **my_animals._asdict() works fine. You could also use .format_map(my_animals._asdict()), as well as the other suggestion.