Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 5
    These are tuples, so your properties method can be written as just return tuple(self), which is more maintainable if in future more fields are added to the namedtuple definition.
    – PaulMcG
    Commented Feb 4, 2016 at 3:52
  • 1
    Also, your namedtuple declaration string does not require commas between the fieldnames, XYZ = namedtuple("XYZ", "x y z") works just as well.
    – PaulMcG
    Commented Feb 4, 2016 at 3:55
  • Thanks @PaulMcGuire. I was trying to think of a really simple add-on to show the inheritance and kind of spaced on that. You're 100% right and it's a great shorthand with other inherited objects, too! I do mention the field names can be comma or space separated -- I prefer CSV from habit Commented Feb 4, 2016 at 3:56
  • 1
    I often use namedtuples for this exact purpose, especially in mathematical code where a function might be highly parametrised and have a bunch of coefficients that only make sense together.
    – detly
    Commented Feb 4, 2016 at 5:03
  • The problem with namedtuple is that they are read-only. You can not do abc.x += 1 or anything like that. Commented Feb 4, 2016 at 11:26