(original) (raw)


On 9 Jun 2014 10:04, "Raymond Hettinger" <raymond.hettinger@gmail.com> wrote:
\>
\>
\> On Jun 7, 2014, at 6:25 AM, R. David Murray <rdmurray@bitdance.com> wrote:
\>
\>>> I guess I could duck-type it based on the \_fields attribute but that
\>>> feels implicit and fragile.
\>>>
\>>> What do you guys suggest?
\>>
\>>
\>> I seem to remember a previous discussion that concluded that duck typing
\>> based on \_fields was the way to go. (It's a public API, despite the \_,
\>> due to name-tuple's attribute namespacing issues.)
\>
\>
\> Yes. That is the recommended approach.
\>
\> IIRC that was Guido's suggestion rather than creating an abstract
\> base class for a named tuple (any tuple-like class with indexable
\> elements that are also accessible using named attributes).

Given the somewhat periodic recurrence of the question, might it be worth making an ABC after all, with "subclass of tuple with a \_fields attribute" as its default check?

"isinstance(obj, collections.NamedTupleABC)" is quite a bit more self-documenting than "isinstance(obj, tuple) and hasattr(obj, '\_fields')"

Cheers,
Nick.

>
\>
\> Raymond
\>
\>
\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
\> Python-Dev mailing list
\> Python-Dev@python.org
\> https://mail.python.org/mailman/listinfo/python-dev
\> Unsubscribe: https://mail.python.org/mailman/options/python-dev/ncoghlan%40gmail.com
\>