4 ms·
One purpose of _source is to make the namedtuple self-documenting -- that has been a considerable benefit over the years and has resulted in many fewer question
by raymondh 9y ago
One purpose of _source is to make the namedtuple self-documenting -- that has been a considerable benefit over the years and has resulted in many fewer questions and issues than we usually get about our other tools.
Another purpose of _source is so that you can write it out to a module. This lets you pre-compute the named tuple for speed and it makes it possible to easily Cythonize it or to customize it (in particular, people like to take out the error checking).
Since the namedtuple() factory function was already creating the source string as part of its operation, just storing it and exposing the attribute had nearly zero additional cost.
That said, don't worry about it, next week I plan to make this a lazily generated attribute so you all can sleep soundly at night :-)
- stuaxo 9y agoIt's interesting + quite nice implementation. I've been using python for 10 years, and vaguely knew namedtuple used exec, but never knew there was a _source attribute. I'm definitely looking forward to faster startup though. I also didn't realise you can't pickle namedtuple by default, which probably cost me a few days on a project, don't think I tracked this down as the reason, but it makes sense now.
- nerdponx 9y agoDoes this mean you're changing how Namedtuples are implemented, or just not saving _source?
- deleted 9y ago[deleted]
- sevensor 9y agoThanks for the clarification! It makes a lot more sense to me now why _source would be part of namedtuple. I think the LWN article must have exaggerated how much of a penalty is being paid for it. Anyway, I appreciate your work on namedtuple -- I use it constantly.