[New-bugs-announce] [issue7439] Bug or expected behavior? I cannot tell.

David W. Lambert report at bugs.python.org
Sat Dec 5 07:08:23 CET 2009


New submission from David W. Lambert <b49P23TIvg at stny.rr.com>:

Raymond Hettinger posted clever Hamming number generator,
http://code.activestate.com/recipes/576961/
which I tried to modify.  The function gives incorrect output when
called as hamming_numbers(shorthand = True).  It seemed reasonable to
expect the two arrangements of statements controlled by the shorthand
boolean to be functionally equivalent. 
http://docs.python.org/3.1/reference/executionmodel.html is relevant,
and makes me think this is not a bug, but I wish it were.  I'd
appreciate your determination.  Thanks, Dave.



from itertools import tee, chain, islice, groupby
from heapq import merge

def hamming_numbers(shorthand = False):

    def deferred_output():
        for i in output:
            yield i

    result, p2, p3, p5 = tee(deferred_output(), 4)

    if shorthand:                   # Lambert modification
        m = [(a*x for x in p) for (a,p,) in ((2,p2),(3,p3),(5,p5))]
        assert m[0] is not m[2]
        merged = merge(*m)
    else:                           # original
        m2 = (2*x for x in p2)
        m3 = (3*x for x in p3)
        m5 = (5*x for x in p5)
        merged = merge(m2, m3, m5)

    combined = chain([1], merged)
    output = (k for k, v in groupby(combined))

    return result


if __name__ == '__main__':
    print(list(islice(hamming_numbers(), 10)))
    print(list(islice(hamming_numbers(True), 10)))

----------
components: Interpreter Core
messages: 95981
nosy: LambertDW
severity: normal
status: open
title: Bug or expected behavior?  I cannot tell.
type: behavior
versions: Python 3.1

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7439>
_______________________________________


More information about the New-bugs-announce mailing list