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.

classification
Title: namedtuple displaying the internal code
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, poornaprudhvi, rhettinger, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2018-02-27 08:55 by poornaprudhvi, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg312984 - (view) Author: poornaprudhvi (poornaprudhvi) Date: 2018-02-27 08:55
>>> from collections import namedtuple
>>> sample = namedtuple('Name','a','b','c')

This is returning the following code as output:

class Name(tuple):
    'Name(a,)'

    __slots__ = ()

    _fields = ('a',)

    def __new__(_cls, a,):
        'Create new instance of Name(a,)'
        return _tuple.__new__(_cls, (a,))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new Name object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 1:
            raise TypeError('Expected 1 arguments, got %d' % len(result))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return 'Name(a=%r)' % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values'
        return OrderedDict(zip(self._fields, self))

    def _replace(_self, **kwds):
        'Return a new Name object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('a',), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % kwds.keys())
        return result

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    __dict__ = _property(_asdict)

    def __getstate__(self):
        'Exclude the OrderedDict from pickling'
        pass

    a = _property(_itemgetter(0), doc='Alias for field number 0')
msg312993 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-02-27 10:23
The behavior of collections.namedtuple has nothing to do with IDLE.  I verified the behavior in 2.7.  In 3.7, one must group the field names, as in a tuple, and the corrected statement has no output.

I had nothing to do with namedtuple development.  I presume that the people who are are aware of the 2.7 behavior.  My guess is that they regaurd the behavior change was either a new version feature change or as too disruptive a fix to backport to 2.7.  Hence, I expect this to be closed.
msg312994 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-02-27 10:37
See https://docs.python.org/2/library/collections.html#collections.namedtuple

namedtuple is called as:
collections.namedtuple(typename, field_names[, verbose=False][, rename=False])

So you are passing in
typename = 'Name'
field_names = 'a'
verbose = 'b'
rename = 'c'

'b' is a True value, so that's why it's showing the output.

You want to be using: 
sample = namedtuple('Name', ['a', 'b', 'c'])
msg312995 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-02-27 10:39
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.
History
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77142
2018-02-27 10:39:27serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg312995
2018-02-27 10:37:05eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg312994

resolution: not a bug
stage: resolved
2018-02-27 10:23:46terry.reedysetnosy: + rhettinger
messages: + msg312993

assignee: terry.reedy ->
components: + Library (Lib), - IDLE
2018-02-27 08:55:10poornaprudhvicreate