[Python-ideas] namedtuple baseclass

Yury Selivanov yselivanov.ml at gmail.com
Sun Jan 12 02:44:06 CET 2014


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/


More information about the Python-ideas mailing list