Leandro Lucarella:

> def NamedTuple(*fields):
>       class T:
>               def __init__(self, *args):
>                       for i in range(len(args)):
>                               setattr(self, fields[i], args[i])
>       return T

That's not enough, those tuples must support the cmp for the heap.

This barely works, but it's not good code yet:

def NamedTuple(*fields):
    class Foo:
        def __init__(self, *args):
            for field, arg in zip(fields, args):
                setattr(self, field, arg)
        def __cmp__(self, other):
            return cmp([getattr(self, f) for f in fields],
                       [getattr(other, f) for f in fields])
    return Foo

Bye,
bearophile

Reply via email to