Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A better namedtuple for Python (benno.id.au)
2 points by tjomk on Dec 3, 2014 | hide | past | favorite | 1 comment


Interesting article. The author asks "So, is the namedfields decorator better than the namedtuple factory function?"

I have another observation. I like namedtuple as a quick-and-dirty class constructor when there are many parameters, as otherwise all of the __init__(self, x, ..) : self.x = x statements get tedious. (Some people have an editor macro for this, but I don't.)

As soon as that object is part of the external API, I have to rewrite it as a regular class, because namedtuple has getitem lookup, and I don't want obj[2] to be part of the public API that I need to maintain forever.

There are times when attribute and index lookup is appropriate. RGB and XYZ values, and of course things like os.stat(), where there was a tuple but where attribute use is better.

I therefore like the idea of

    @namedfields("spam eggs ham")
    class Viking:
      pass
where the [0], [1], and [2] aren't built-in.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: