Huffman coding comparison

bearophile bearophileHUGS at lycos.com
Fri May 28 17:45:27 PDT 2010


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


More information about the Digitalmars-d mailing list