Users of namedtuple: do you use the _source attribute?
Ethan Furman
ethan at stoneleaf.us
Mon Jul 17 19:37:57 EDT 2017
On 07/17/2017 12:44 PM, Rob Gaddi wrote:
> 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']).
>>
>> [...]
>>
>> Is there anyone here who uses the namedtuple _source attribute?
> 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?
I can't answer that question, but I can say my aenum library [1][2] uses the same metaclass technique as the new Enum
type, and also supports doc strings and default arguments in the class-based format.
--
~Ethan~
[1] https://pypi.python.org/pypi/aenum (works back to at least 2.7)
[2] Disclosure: I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum)
library.
More information about the Python-list
mailing list