[Python-ideas] namedtuple baseclass

Yury Selivanov yselivanov.ml at gmail.com
Sun Jan 12 00:04:06 CET 2014


Hello all,

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. Basically, the only way of checking if an
object
is an instance of 'namedtuple' is to do "isinstance(o, tuple) and
hasattr(o, '_fields')".

One possible approach would be to:

1. Rename 'namedtuple' function to '_namedtuple'

2. Add a class 'namedtuple(tuple)', with its '__new__' method proxying
'_namedtuple' function

3. Modify the class template to derive namedtuples from the 'namedtuple'
class, instead of 'tuple'

This way, it's possible to simple write 'isinstance(o, namedtuple)'.

I have a working patch that implements the above logic (all python
unittests pass),
so if you find this useful I can start an issue on bugs.python.org.

Thank you,
Yury
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140111/7cde7f7e/attachment.html>


More information about the Python-ideas mailing list