Users of namedtuple: do you use the _source attribute?

Rob Gaddi rgaddi at highlandtechnology.invalid
Mon Jul 17 15:44:09 EDT 2017


On 07/17/2017 09:57 AM, Steve D'Aprano wrote:
> collections.namedtuple generates a new class using exec, and records the source
> code for the class as a _source attribute.
> 
> Although it has a leading underscore, it is actually a public attribute. The
> leading underscore distinguishes it from a named field potentially
> called "source", e.g. namedtuple("klass", ['source', 'destination']).
> 
> 
> There is some discussion on Python-Dev about:
> 
> - changing the way the namedtuple class is generated which may
>    change the _source attribute
> 
> - or even dropping it altogether
> 
> in order to speed up namedtuple and reduce Python's startup time.
> 
> 
> Is there anyone here who uses the namedtuple _source attribute?
> 
> My own tests suggest that changing from the current implementation to one
> similar to this recipe here:
> 
> https://code.activestate.com/recipes/578918-yet-another-namedtuple/
> 
> which only uses exec to generate the __new__ method, not the entire class, has
> the potential to speed up namedtuple by a factor of four.
> 

I use namedtuple a lot, and never even HEARD of _source.

That said, it sure feels (as someone who hasn't tried it) like there's a 
straightforward namedtuple implementation that calls type() directly 
rather than having to exec.  I know that exec-gunshyness is overblown, 
but is there a simple answer as to why it's necessary here?

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list