This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author John Crawford
Recipients John Crawford
Date 2018-02-15.18:10:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1518718232.91.0.467229070634.issue32854@psf.upfronthosting.co.za>
In-reply-to
Content
At present, `collections.namedtuple` does not support `**` map unpacking despite being a mapping style data structure.  For example:

    >>> from collections import namedtuple
    >>> A = namedtuple("A", "a b c")
    >>> a = A(10, 20, 30)
    >>> def t(*args, **kwargs):
    ...     print(f'args={args!r}, kwargs={kwargs!r}')
    ...
    >>> t(*a)
    args=(10, 20, 30), kwargs={}
    >>> t(**a)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: t() argument after ** must be a mapping, not A
    >>> 

No doubt, the lack of current support is due to namespace conflicts that result from trying to provide a `keys` method amidst also supporting attribute-style access to the `namedtuple` class.  As we can see, providing a `keys` attribute in the `namedtuple` produces an interesting result:

    >>> Record = namedtuple("Record", "title keys description")
    >>> t(**Record(1, 2, 3))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: attribute of type 'int' is not callable
    >>>

To me, this calls out a design flaw in the `**` unpacking operator where it depends on a method that uses a non-system naming convention.  It would make far more sense for the `**` operator to utilize a `__keys__` method rather than the current `keys` method.  After all, the `__<methodname>__` naming convention was introduced to avoid namespace conflict problems like this one.
History
Date User Action Args
2018-02-15 18:10:32John Crawfordsetrecipients: + John Crawford
2018-02-15 18:10:32John Crawfordsetmessageid: <1518718232.91.0.467229070634.issue32854@psf.upfronthosting.co.za>
2018-02-15 18:10:32John Crawfordlinkissue32854 messages
2018-02-15 18:10:32John Crawfordcreate