tags:

views:

37

answers:

1

Suppose I have a namedtuple like

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

Why is it that I construct a single object via

>>> Point(3,4)

yet when I want to apply Point via map, I have to call

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

I suspect this has something to do with classmethods, perhaps, and am hoping that in figuring this out I'll learn more about them as well. Thanks in advance.

+2  A: 

Point._make takes a tuple as its sole argument. Your map call is equivalent to [Point._make((3, 4)), Point._make((5, 6))].

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

Aaron Gallagher
You can also use starmap from itertools - list(starmap(Point, [[1,2], [3, 4]]))
lazy1
There's a dozen other ways to do it, but a list comprehension is the clearest in what it does exactly.
Aaron Gallagher
Thanks, everyone. This really cleared up the issue for me.
Rick