Package grizzled :: Package collections :: Module tuple
[frames] | no frames]

Module tuple

grizzled.collections.tuple contains some useful tuple-related classes and functions.
Functions
class
namedtuple(typename, fieldnames, verbose=False)
Returns a new subclass of tuple with named fields.
Function Details

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]          # prints 1
print p1[1]          # prints 22
x, y = p0            # x=10, y=20
print p0.x           # prints 10
d = p2._asdict()     # convert to dictionary
print d['x']         # prints 1
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