[Python-ideas] namedtuple baseclass

Andrew Barnert abarnert at yahoo.com
Sun Jan 12 08:53:54 CET 2014


It sounds like the consensus there wasn't to have a base class for namedtuple, but instead to have an abc that all namedtuples, and C namedtuple-like types, would be registered with, and that would have no API beyond that of Sequence.

If I understand the original request in this thread, I'm not sure this would satisfy the use case. 

He's looking to detect namedtuples so he can extract their names along with their values. Which is a perfectly reasonable thing to do for the kind of reflective code he wants to write. It would presumably use code like this:

    if isinstance(x, NamedTuple);
        d = OrderedDict(zip(x._fields, x))
        do_stuff(d)

But that won't work with any abstract NamedTuple, only one that has a _fields member that lists the field names. So you'd need to write this:

    if isinstance(NamedTuple):
        try:
            d = OrderedDict(zip(x._fields, x))
        except AttributeError:
            whoops, it's an os.stat_result or something
        else:
            do_stuff(d)

And at that point, the isinstance check isn't helping anything over the duck typing on _fields, which you can already do today.

So to satisfy this use case, you'd either need an actual namedtuple base class instead of an abc, or an abc that adds some API for getting the field names (or name-value pairs). Either of which seems reasonable--except for the odd quirk of having a public API in a class that's prefixed with an underscore. (If it's not prefixed with an underscore, it can conflict with a field name, which defeats the whole purpose of namedtuple.)

Sent from a random iPhone

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

> Hi Eric,
> 
> Thank you very much for bringing this up. I couldn't find that issue (perhaps,
> because I was looking for an open ticket).
> 
> From the discussion there, it seems that Raymond and Guido agreed to
> have a common base class for namedtuple for py3.3; however, that was in
> 2010/2011.
> 
> Perhaps, any doubts that existed at that time are not the case now?
> 
> Thanks,
> Yury
> 
> 
> 
> On Sat, Jan 11, 2014 at 8:27 PM, Eric V. Smith <eric at trueblade.com> wrote:
>> See also http://bugs.python.org/issue7796 for a discussion of this issue.
>> 
>> --
>> Eric.
>> 
>>> On Jan 11, 2014, at 8:05 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>>> 
>>>> On Sat, Jan 11, 2014 at 06:04:06PM -0500, Yury Selivanov wrote:
>>>> 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')".
>>> 
>>> Let me see if I understand your use-case. You want to dynamically
>>> dispatch on various objects. Given two objects:
>>> 
>>> p1 = (23, 42)
>>> p2 = namedtuple("pair", "a b")(23, 42)
>>> assert p1 == p2
>>> 
>>> 
>>> you want to dispatch p1 and p2 differently. Is that correct?
>>> 
>>> 
>>> Then, given a third object:
>>> 
>>> class Person(namedtuple("Person", "name sex age occupation id")):
>>>   def say_hello(self):
>>>       print("Hello %s" % self.name)
>>> 
>>> p3 = Person("Fred Smith", "M", 35, "nurse", 927056)
>>> 
>>> 
>>> you want to dispatch p2 and p3 the same. Is that correct?
>>> 
>>> If I am correct, I wonder what sort of code you are writing that wants
>>> to treat p1 and p2 differently, and p2 and p3 the same. To me, this
>>> seems ill-advised. Apart from tuple (and object), p2 and p3 should not
>>> share a common base class, because they have nothing in common.
>>> 
>>> 
>>> [...]
>>>> This way, it's possible to simple write 'isinstance(o, namedtuple)'.
>>> 
>>> I am having difficulty thinking of circumstances where I would want to
>>> do that.
>>> 
>>> -1 on the idea.
>>> 
>>> 
>>> --
>>> Steven
>>> _______________________________________________
>>> Python-ideas mailing list
>>> Python-ideas at python.org
>>> https://mail.python.org/mailman/listinfo/python-ideas
>>> Code of Conduct: http://python.org/psf/codeofconduct/
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list