[Python-ideas] namedtuple baseclass

Raymond Hettinger raymond.hettinger at gmail.com
Sun Jan 12 21:01:35 CET 2014


On Jan 11, 2014, at 11:04 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:

> I propose to add a baseclass for all namedtuples. Right now 'namedtuple'
> function dynamically creates a class derived from 'tuple', which complicates
> things like dynamic dispatch.

A named tuple is a protocol, not a class. 

Here's the glossary entry:

'''
named tuple
Any tuple-like class whose indexable elements are also accessible using named attributes (for example, time.localtime() returns a tuple-like object where the year is accessible either with an index such as t[0] or with a named attribute like t.tm_year).

A named tuple can be a built-in type such as time.struct_time, or it can be created with a regular class definition. A full featured named tuple can also be created with the factory function collections.namedtuple(). The latter approach automatically provides extra features such as a self-documenting representation like Employee(name='jones', title='programmer').


'''

> Basically, the only way of checking if an object
> is an instance of 'namedtuple' is to do "isinstance(o, tuple) and hasattr(o, '_fields')".


Yes, that is the correct way of doing it.

ABCs weren't meant to replace all instances of duck typing.


Raymond


P.S. Here's a link to previous discussion on the subject:  http://bugs.python.org/issue7796


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140112/db088d40/attachment.html>


More information about the Python-ideas mailing list