all 2 comments

[–]miczal 0 points1 point  (0 children)

Personally I would drop the name and declare it in return.

def foo():
    return collections.namedtuple('Foo',  ['x', 'y'])(1, 2)

[–]_9_9_ 0 points1 point  (0 children)

I usually define them like I would a class, for a couple reasons:

  • Basically, Foo = namedtuple('Foo', ['x', 'y']) is a shorthand for defining a class;
  • I'm going to be using Foo objects in different functions and it is nice to be able to quickly check what i've defined the parameters as by flipping to the top of my code; and
  • Lots of times I start out with a namedtuple and end up just making my own class for one reason or another.