Add nicer docstrings to namedtuples().
Provides better tooltips and looks better in help().
This commit is contained in:
parent
08090bf36a
commit
9bd3508530
|
@ -679,6 +679,7 @@ Example:
|
||||||
_fields = ('x', 'y')
|
_fields = ('x', 'y')
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
def __new__(_cls, x, y):
|
def __new__(_cls, x, y):
|
||||||
|
'Create a new instance of Point(x, y)'
|
||||||
return _tuple.__new__(_cls, (x, y))
|
return _tuple.__new__(_cls, (x, y))
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -690,6 +691,7 @@ Example:
|
||||||
return result
|
return result
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
'Return a nicely formatted representation string'
|
||||||
return 'Point(x=%r, y=%r)' % self
|
return 'Point(x=%r, y=%r)' % self
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
def _asdict(self):
|
def _asdict(self):
|
||||||
|
@ -704,10 +706,11 @@ Example:
|
||||||
return result
|
return result
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
def __getnewargs__(self):
|
def __getnewargs__(self):
|
||||||
|
'Return self as a plain tuple. Used by copy and pickle.'
|
||||||
return tuple(self)
|
return tuple(self)
|
||||||
<BLANKLINE>
|
<BLANKLINE>
|
||||||
x = _property(_itemgetter(0))
|
x = _property(_itemgetter(0), doc='Alias for field number 0')
|
||||||
y = _property(_itemgetter(1))
|
y = _property(_itemgetter(1), doc='Alias for field number 1')
|
||||||
|
|
||||||
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
|
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
|
||||||
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)
|
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)
|
||||||
|
|
|
@ -230,6 +230,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
|
||||||
__slots__ = () \n
|
__slots__ = () \n
|
||||||
_fields = %(field_names)r \n
|
_fields = %(field_names)r \n
|
||||||
def __new__(_cls, %(argtxt)s):
|
def __new__(_cls, %(argtxt)s):
|
||||||
|
'Create new instance of %(typename)s(%(argtxt)s)'
|
||||||
return _tuple.__new__(_cls, (%(argtxt)s)) \n
|
return _tuple.__new__(_cls, (%(argtxt)s)) \n
|
||||||
@classmethod
|
@classmethod
|
||||||
def _make(cls, iterable, new=tuple.__new__, len=len):
|
def _make(cls, iterable, new=tuple.__new__, len=len):
|
||||||
|
@ -239,6 +240,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
|
||||||
raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
|
raise TypeError('Expected %(numfields)d arguments, got %%d' %% len(result))
|
||||||
return result \n
|
return result \n
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
'Return a nicely formatted representation string'
|
||||||
return '%(typename)s(%(reprtxt)s)' %% self \n
|
return '%(typename)s(%(reprtxt)s)' %% self \n
|
||||||
def _asdict(self):
|
def _asdict(self):
|
||||||
'Return a new OrderedDict which maps field names to their values'
|
'Return a new OrderedDict which maps field names to their values'
|
||||||
|
@ -250,9 +252,10 @@ def namedtuple(typename, field_names, verbose=False, rename=False):
|
||||||
raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
|
raise ValueError('Got unexpected field names: %%r' %% kwds.keys())
|
||||||
return result \n
|
return result \n
|
||||||
def __getnewargs__(self):
|
def __getnewargs__(self):
|
||||||
|
'Return self as a plain tuple. Used by copy and pickle.'
|
||||||
return tuple(self) \n\n''' % locals()
|
return tuple(self) \n\n''' % locals()
|
||||||
for i, name in enumerate(field_names):
|
for i, name in enumerate(field_names):
|
||||||
template += ' %s = _property(_itemgetter(%d))\n' % (name, i)
|
template += " %s = _property(_itemgetter(%d), doc='Alias for field number %d')\n" % (name, i, i)
|
||||||
if verbose:
|
if verbose:
|
||||||
print template
|
print template
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue