Message256995
I agree that namedtuples having __dict__ is probably more trouble than benefit. But in my view, that's no reason for _asdict to not work correctly. The whole point of separate function (even going through the pain of underscore-starting public API, since everything else is even bigger pain) is that we sidestep the question of vars() and other standard Python hooks, and provide our way of extracting a namedtuple's namespace, for ones who need it.
Of course, the fix/workaround is trivial, as Raymond says: just
def _asdict(self):
return collections.OrderedDict(zip(self._fields, self))
That way, it doesn't matter whether self has a `__dict__` or not.
If you think it shouldn't be done, the documentation really needs to be changed. Current wording is very misleading, showing that `_asdict` works on a class having `__slots__ = ()`, but containing no explanation of that attribute being _necessary_ for `_asdict` to work. (It just says "This helps keep memory requirements low by preventing the creation of instance dictionaries.") |
|
Date |
User |
Action |
Args |
2015-12-25 15:56:27 | veky | set | recipients:
+ veky, rhettinger, doko, larry, python-dev, eric.snow, nchammas, Samuel Isaacson |
2015-12-25 15:56:27 | veky | set | messageid: <1451058987.25.0.865373080981.issue24931@psf.upfronthosting.co.za> |
2015-12-25 15:56:27 | veky | link | issue24931 messages |
2015-12-25 15:56:26 | veky | create | |
|