[New-bugs-announce] [issue32259] Misleading "not iterable" Error Message when generator return a "simple" type, and a tuple is expected

Camion report at bugs.python.org
Sat Dec 9 07:10:32 EST 2017

New submission from Camion <camion_spam-pybugs at yahoo.com>:

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].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.

components: Interpreter Core
messages: 307892
nosy: Camion
priority: normal
severity: normal
status: open
title: Misleading "not iterable" Error Message when generator return a "simple" type, and a tuple is expected
type: behavior
versions: Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list