mirror of
https://github.com/python/cpython.git
synced 2025-11-22 19:46:48 +00:00
Code cleanups
This commit is contained in:
parent
1eda5c9a83
commit
491f7070f1
2 changed files with 105 additions and 76 deletions
|
|
@ -601,47 +601,49 @@ Example:
|
|||
|
||||
>>> Point = namedtuple('Point', ['x', 'y'], verbose=True)
|
||||
class Point(tuple):
|
||||
'Point(x, y)'
|
||||
'Point(x, y)'
|
||||
<BLANKLINE>
|
||||
__slots__ = ()
|
||||
__slots__ = ()
|
||||
<BLANKLINE>
|
||||
_fields = ('x', 'y')
|
||||
_fields = ('x', 'y')
|
||||
<BLANKLINE>
|
||||
def __new__(_cls, x, y):
|
||||
'Create a new instance of Point(x, y)'
|
||||
return _tuple.__new__(_cls, (x, y))
|
||||
def __new__(_cls, x, y):
|
||||
'Create a new instance of Point(x, y)'
|
||||
return _tuple.__new__(_cls, (x, y))
|
||||
<BLANKLINE>
|
||||
@classmethod
|
||||
def _make(cls, iterable, new=tuple.__new__, len=len):
|
||||
'Make a new Point object from a sequence or iterable'
|
||||
result = new(cls, iterable)
|
||||
if len(result) != 2:
|
||||
raise TypeError('Expected 2 arguments, got %d' % len(result))
|
||||
return result
|
||||
@classmethod
|
||||
def _make(cls, iterable, new=tuple.__new__, len=len):
|
||||
'Make a new Point object from a sequence or iterable'
|
||||
result = new(cls, iterable)
|
||||
if len(result) != 2:
|
||||
raise TypeError('Expected 2 arguments, got %d' % len(result))
|
||||
return result
|
||||
<BLANKLINE>
|
||||
def __repr__(self):
|
||||
'Return a nicely formatted representation string'
|
||||
return 'Point(x=%r, y=%r)' % self
|
||||
def __repr__(self):
|
||||
'Return a nicely formatted representation string'
|
||||
return 'Point(x=%r, y=%r)' % self
|
||||
<BLANKLINE>
|
||||
def _asdict(self):
|
||||
'Return a new OrderedDict which maps field names to their values'
|
||||
return OrderedDict(zip(self._fields, self))
|
||||
def _asdict(self):
|
||||
'Return a new OrderedDict which maps field names to their values'
|
||||
return OrderedDict(zip(self._fields, self))
|
||||
<BLANKLINE>
|
||||
__dict__ = property(_asdict)
|
||||
__dict__ = property(_asdict)
|
||||
<BLANKLINE>
|
||||
def _replace(_self, **kwds):
|
||||
'Return a new Point object replacing specified fields with new values'
|
||||
result = _self._make(map(kwds.pop, ('x', 'y'), _self))
|
||||
if kwds:
|
||||
raise ValueError('Got unexpected field names: %r' % kwds.keys())
|
||||
return result
|
||||
def _replace(_self, **kwds):
|
||||
'Return a new Point object replacing specified fields with new values'
|
||||
result = _self._make(map(kwds.pop, ('x', 'y'), _self))
|
||||
if kwds:
|
||||
raise ValueError('Got unexpected field names: %r' % kwds.keys())
|
||||
return result
|
||||
<BLANKLINE>
|
||||
def __getnewargs__(self):
|
||||
'Return self as a plain tuple. Used by copy and pickle.'
|
||||
return tuple(self)
|
||||
def __getnewargs__(self):
|
||||
'Return self as a plain tuple. Used by copy and pickle.'
|
||||
return tuple(self)
|
||||
<BLANKLINE>
|
||||
x = _property(_itemgetter(0), doc='Alias for field number 0')
|
||||
<BLANKLINE>
|
||||
y = _property(_itemgetter(1), doc='Alias for field number 1')
|
||||
<BLANKLINE>
|
||||
x = _property(_itemgetter(0), doc='Alias for field number 0')
|
||||
y = _property(_itemgetter(1), doc='Alias for field number 1')
|
||||
|
||||
>>> p = Point(11, y=22) # instantiate with positional or keyword arguments
|
||||
>>> p[0] + p[1] # indexable like the plain tuple (11, 22)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue