Using `map` to create multiple named tuples

Suppose I have namedtuple like

>>> Point = namedtuple('Point','x y')

Why am I creating one object through

>>> Point(3,4)

but when I want to apply Point through the map, I need to call

>>> map(Point._make,[(3,4),(5,6)])

I suspect that this has something to do with classmethods, perhaps, and I hope that by understanding this, I will learn more about them. Thanks in advance.

+3
source share
1 answer

Point._maketakes a tuple as the only argument. Your call is mapequivalent [Point._make((3, 4)), Point._make((5, 6))].

Using list comprehension makes this more obvious: it [Point(*t) for t in [(3, 4), (5, 6)]]achieves the same effect.

+5
source

Source: https://habr.com/ru/post/1771846/


All Articles