Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b0d3c6

Browse files
committed
Add nice docstrings to namedtuples.
1 parent 41fe615 commit 7b0d3c6

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

Doc/library/collections.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ Example:
653653
_fields = ('x', 'y')
654654
<BLANKLINE>
655655
def __new__(_cls, x, y):
656+
'Create a new instance of Point(x, y)'
656657
return _tuple.__new__(_cls, (x, y))
657658
<BLANKLINE>
658659
@classmethod
@@ -664,6 +665,7 @@ Example:
664665
return result
665666
<BLANKLINE>
666667
def __repr__(self):
668+
'Return a nicely formatted representation string'
667669
return 'Point(x=%r, y=%r)' % self
668670
<BLANKLINE>
669671
def _asdict(self):
@@ -678,10 +680,11 @@ Example:
678680
return result
679681
<BLANKLINE>
680682
def __getnewargs__(self):
683+
'Return self as a plain tuple. Used by copy and pickle.'
681684
return tuple(self)
682685
<BLANKLINE>
683-
x = _property(_itemgetter(0))
684-
y = _property(_itemgetter(1))
686+
x = _property(_itemgetter(0), doc='Alias for field number 0')
687+
y = _property(_itemgetter(1), doc='Alias for field number 1')
685688

686689
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
687690
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)

Lib/collections.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
233233
__slots__ = () \n
234234
_fields = %(field_names)r \n
235235
def __new__(_cls, %(argtxt)s):
236+
'Create new instance of %(typename)s(%(argtxt)s)'
236237
return _tuple.__new__(_cls, (%(argtxt)s)) \n
237238
@classmethod
238239
def _make(cls, iterable, new=tuple.__new__, len=len):
@@ -242,6 +243,7 @@ def _make(cls, iterable, new=tuple.__new__, len=len):
242243
raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
243244
return result \n
244245
def __repr__(self):
246+
'Return a nicely formatted representation string'
245247
return '%(typename)s(%(reprtxt)s)' %% self \n
246248
def _asdict(self):
247249
'Return a new OrderedDict which maps field names to their values'
@@ -253,9 +255,10 @@ def _replace(_self, **kwds):
253255
raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
254256
return result \n
255257
def __getnewargs__(self):
258+
'Return self as a plain tuple. Used by copy and pickle.'
256259
return tuple(self) \n\n''' % locals()
257260
for i, name in enumerate(field_names):
258-
template += ' %s = _property(_itemgetter(%d))\n' % (name, i)
261+
template += " %s = _property(_itemgetter(%d), doc='Alias for field number %d')\n" % (name, i, i)
259262
if verbose:
260263
print(template)
261264

0 commit comments

Comments
 (0)