Users of namedtuple: do you use the _source attribute?

Dan Strohl D.Strohl at F5.com
Mon Jul 17 13:56:37 EDT 2017


I have never used it personally.  It always looked interesting, but I never ran into a need to generate the source for it.

-----Original Message-----
From: Python-list [mailto:python-list-bounces+d.strohl=f5.com at python.org] On Behalf Of Steve D'Aprano
Sent: Monday, July 17, 2017 9:58 AM
To: python-list at python.org
Subject: Users of namedtuple: do you use the _source attribute?

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.



--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure enough, things got worse.

--
https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list