namedtuple(typename,
fieldnames,
verbose=False)
|
|
Returns a new subclass of tuple with named fields. If running under Python
2.6 or newer, this function is nothing more than an alias for the
standard Python collections.namedtuple() function. Otherwise,
this function is a local implementation, adapted from an
ActiveState namedtuple recipe.
Usage:
Point = namedtuple('Point', 'x y')
p0 = Point(10, 20)
p1 = Point(11, y=22)
p2 = Point(x=1, y=2)
print p2[0]
print p1[1]
x, y = p0
print p0.x
d = p2._asdict()
print d['x']
- Parameters:
typename (str) - Name for the returned class
fieldnames (str or sequence) - A single string with each field name separated by whitespace and/or
commas (e.g., 'x y' or 'x, y'). Alternatively, fieldnames can
be a sequence of strings such as ['x', 'y'].
verbose (bool) - If True, the class definition will be printed to standard
output before being returned
- Returns: class
- The named tuple class
- Raises:
ValueError - Bad parameters
|