[issue32961] namedtuple displaying the internal code

Serhiy Storchaka report at bugs.python.org
Tue Feb 27 05:39:27 EST 2018


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

In 2.7 namedtuple() takes four arguments.

    namedtuple(typename, field_names, verbose=False, rename=False)

A sequence of field names should be passed as the second argument. In you case you pass four argumens: 'a' as field names, 'b' as the verbose flag, and 'c' as the rename flag. Since 'b' has true boolean value, namedtuple() outputs the source used for generating a named tuple with a single field 'a'.

In Python 3.6+ verbose and rename are keyword-only parameters (see issue25628) and this error can be caught earlier:

>>> sample = namedtuple('Name','a','b','c')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: namedtuple() takes 2 positional arguments but 4 were given

This change can't be backported to 2.7 for two reasons:

1) There is no syntax support for keyword-only parameters in 2.7.
2) This can break a correct code which passes flags as positional arguments.

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32961>
_______________________________________


More information about the Python-bugs-list mailing list