This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Camion
Recipients Camion
Date 2017-12-09.12:10:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1512821432.36.0.213398074469.issue32259@psf.upfronthosting.co.za>
In-reply-to
Content
I'm new with Python and I've been blocked for day on a "TypeError: 'Fraction' object is not iterable" error message, while the problem turned out to be completely different.

I don't even know to what programming case this message would have been the right interpretation, but in this situation, it was completely wrong and I believe it can cause much loss of time for inexperienced python programmer (at least, I believe it should document alternative causes)

Here is a sample code to show how misleading it can be (It's a program to compute pi with fractions)

from fractions import Fraction


def order(x):
    r, old_r, n, old_n = 2, 1, 1, 0
    while (x>=r):
        r, old_r, n, old_n = r*r, r, 2*n, n
    return order(x >> old_n) + old_n if old_n > 0 else 0


def terms(m, n, i):
    return Fraction(4 * m, n**(2*i+1) * (2*i+1))


def terms_generator(exp_prec):
    ws = [ [terms(parm[1], parm[2], 0), 0] + list(parm)
          for parm in ((1, 44, 57),
                       (1, 7, 239),
                       (-1, 12, 682),
                       (1, 24, 12943))]
    digits = 0
    while digits<exp_prec:
        curws = max(ws, key=lambda col: col[0])
        digits = int(0.30103 *
                     (order(curws[0].denominator))
                      - order(curws[0].numerator))
        yield curws[2] * curws[0] #, digits
        curws[2] = -curws[2]                
        curws[1] += 1
        curws[0] = terms(curws[3], curws[4], curws[1])



expected_precision = 100

pi = 0
for term, dgts in terms_generator(expected_precision):
    pi += term

print("{} digits".format(dgts))
print("pi = 3.{}".format(int((pi-3)*10**expected_precision)))




Obviously, the problem came from having forgotten one argument in the "yield" line, which has nothing to do with a problem of iterability of my type.
History
Date User Action Args
2017-12-09 12:10:32Camionsetrecipients: + Camion
2017-12-09 12:10:32Camionsetmessageid: <1512821432.36.0.213398074469.issue32259@psf.upfronthosting.co.za>
2017-12-09 12:10:32Camionlinkissue32259 messages
2017-12-09 12:10:31Camioncreate