开发者

using `map` to construct multiple named tuples

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜