Python syntax for namedtuple

前端 未结 4 1239
情深已故
情深已故 2021-01-12 17:44

I see that the Python syntax for a namedtuple is:

Point = namedtuple(\'Point\', [\'x\', \'y\'])

Why isn\'t it simpler like so:



        
4条回答
  •  误落风尘
    2021-01-12 18:21

    The class should have a name and know it. And it doesn't see the variable you assign it to, so it can't use that. Plus you could call it something else or even nothing at all:

    c = namedtuple('Point', ['x', 'y'])
    do_something_with_this(namedtuple('Point', ['x', 'y']))
    

    Speaking of simpler syntax, you can also write it like this:

    namedtuple('Point', 'x y')
    

提交回复
热议问题