def NamedSequences( className, *_names, **_kwds ):
    """NamedSequences( className[, 'name_1'[,...[,
                       name_N = defaultValue_1[, ... ]]]] )

    Construct a new class that contains only the named elements.
    This is useful in cases where you know you're going to have a ton
    of instances for a class and wish to conserve memory by avoiding
    the overhead of a per instance dictionary.

    Instances of the returned class are Python sequences.

        NamedSequences(
            __name__, 'MyClass',
            'fieldName1', ..., # argument field names
            fieldNameN = defaultValue1, ...)

    Keyword arguments are unordered dict's so if you wish to control
    field ordering you MUST specify your field names twice, once in
    the positional argument list area (to define the ordering), and
    the second time in the keyword area to specify that field's
    default value.  Field names defined only as keyword arguments are
    placed in ascending order after all field names specified as
    positional arguments.  Duplicate field names are dropped with
    priority given to the first appearance of the name.

    Keyword arguments beginning and ending with a double underscore
    are Python reserved words.  These will be inserted directly into
    the class __dict__ rather than added as regular field names.

    For example you can redefine the module that the class belongs to
    using the following pattern:

        Point = NamedSequences('Point', 'x', 'y', __module__ = __name__ )

    though this is actually redundant as the default is the module
    from which this function was called.  You can even specify your
    own documentation for the class using "__doc__ = '<my_docs>'".
    """
    # versatile arguments: (klass, 'x y'), (klass, 'x', 'y') or (klass, 'x,y')
    if len( _names ) == 1 and isinstance( _names[0], basestring ):
        _names = _names[0].replace( ',', ' ' ).split()
    _names = tuple( map( str, _names ) )

    def is_identifier( s ):
        try:
            class Tmp( object ): __slots__ = (s,)
        except TypeError: return False
        else: return not keyword.iskeyword( s )

    if not is_identifier( className ):
        raise ValueError( 'class name "%s" is not a valid identifier' % className )
    for name in _names + tuple( _kwds.keys() ):
        if not is_identifier( name ):
            raise ValueError( 'field name "%s" is not a valid identifier' % className )

    # Extract Python reserved words
    extras = dict( ( k, v ) for k, v in _kwds.iteritems() if k.startswith( '__' ) and k.endswith( '__' ) )
    _kwds = copy.deepcopy( _kwds )                 # no messing with defaults
    for k, v in extras.iteritems(): _kwds.pop( k ) # remove reserved words
    try: extras.setdefault( '__module__', sys._getframe( 1 ).f_globals.get( '__name__', '__main__' ) )
    except ( AttributeError, ValueError ): pass # no getting at the module in this env...
    _slots = tuple( unique( _names + tuple( sorted( _kwds.keys() ) ) ) ) # there can be only one...

    class SequenceClass( object ):
        """This class is the superclass of the class generated by
        "NamedSequences".  Each call to "NamedSequences" creates a
        new "SequenceClass" superclass.
        """
        __slots__ = ()
        def __init__( self, *argv, **kwds ):
            for n, v in zip( _slots, argv ): setattr( self, n, v )   # apply positional args 1st
            for n, v in kwds.iteritems(): setattr( self, n, v )     # apply keyword args 2nd
        def __cmp__( self, other ):
            if self.__class__ is other.__class__:
                return cmp( tuple( self ), tuple( other ) )
            else:
                raise TypeError, "requires a '%s' object but received a '%s'" % ( self.__class__.__name__, other.__class__.__name__ )
        def __getattr__( self, n ):
            """Defer applying class defaults until they are actually needed"""
            try: result = _kwds[n]
            except KeyError:
                if n in _slots:
                    result = None
                else: raise
            object.__setattr__( self, n, result )
            return result
        def __getitem__( self, i ): return getattr( self, _slots[i] )
        def __getnewargs__( self ): return tuple( self )
        def __setitem__( self, i, v ): setattr( self, _slots[i], v )
        def __iter__( self ): return iter( getattr( self, n ) for n in _slots )
        def __len__( self ):  return len( _slots )
        def __repr__( self ): return self.__class__.__name__ + '(' + ', '.join( n + ' = ' + repr( getattr( self, n ) ) for n in _slots ) + ')'
        def __str__( self ):  return self.__class__.__name__ + '(' + ', '.join( repr( getattr( self, n ) ) for n in _slots ) + ')'
        def _asdict( self ):  return dict( ( n, getattr( self, n ) ) for n in _slots )
        def _replace( self, **kwds ): return type( self )( *self, **kwds )

    klass = SequenceClass
    extras.setdefault( '__doc__', '%s%s => instance' % ( className, repr( _slots ).replace( "'", "" ) ) )
    extras.update( {
        '__name__': className,
        '__slots__': _slots
    } )
    return type( klass )( className, ( klass, ), extras )
NamedSequences( className[, 'name_1'[,...[,
                       name_N = defaultValue_1[, ... ]]]] )

    Construct a new class that contains only the named elements.
    This is useful in cases where you know you're going to have a ton
    of instances for a class and wish to conserve memory by avoiding
    the overhead of a per instance dictionary.

    Instances of the returned class are Python sequences.

        NamedSequences(
            __name__, 'MyClass',
            'fieldName1', ..., # argument field names
            fieldNameN = defaultValue1, ...)

    Keyword arguments are unordered dict's so if you wish to control
    field ordering you MUST specify your field names twice, once in
    the positional argument list area (to define the ordering), and
    the second time in the keyword area to specify that field's
    default value.  Field names defined only as keyword arguments are
    placed in ascending order after all field names specified as
    positional arguments.  Duplicate field names are dropped with
    priority given to the first appearance of the name.

    Keyword arguments beginning and ending with a double underscore
    are Python reserved words.  These will be inserted directly into
    the class __dict__ rather than added as regular field names.

    For example you can redefine the module that the class belongs to
    using the following pattern:

        Point = NamedSequences('Point', 'x', 'y', __module__ = __name__ )

    though this is actually redundant as the default is the module
    from which this function was called.  You can even specify your
    own documentation for the class using "__doc__ = '<my_docs>'".
    
recipes/Python/577473_Named_Sequences_environments_containing_large/recipe-577473.py