What's up with this code?

plahey at alumni.caltech.edu plahey at alumni.caltech.edu
Thu Feb 23 00:56:57 EST 2006


Scott,

this was a really clever catch, but I don't agree with the solution.
The problem is your assumption of how zip/izip _must_ work.  I don't
think there is any contract that states that they will always advance
the first argument first and the second argument second... that is an
implementation detail (the end result, ignoring iterator state, is the
same regardless of the order in which you advance things).  Given this,
I think using izip is simply a bad idea.

How about something like this:

    def __add__(self, other):
        rcoefs1 = reversed(self.coefs)
        rcoefs2 = reversed(other.coefs)
        minlen = min(len(rcoefs1), len(rcoefs2))
        coefs = [rcoefs1.next()+rcoefs2.next() for i in xrange(minlen)]
        coefs.extend(rcoefs1)
        coefs.extend(rcoefs2)
        if coefs[-1] != 0:
            return Polynomial(reversed(coefs), False, False)
        else:
            return Polynomial(reversed(coefs), False)




More information about the Python-list mailing list