Python is faster than C

Raymond Hettinger python at rcn.com
Sun Apr 4 00:44:38 EST 2004


[Armin Rigo] 
> >>> enumerate([6,7,8,9])         # uh ?
> <enumerate object at 0x401a102c>

This got me thinking about how much I would like to see the contents
of an iterator at the interactive prompt.

I wonder if the read-eval-print loop could be trained to make a better
display:

# rough pseudo-code sketch
while 1:
    command = raw_input()
    result = eval(command)
    if result is None:
        continue
    if is_iterator(result):
        result, copy = itertools.tee(result)
        print "<%s object at 0x%8x:" % 
                (type(result).__name__, id(result)),
        for elem in itertools.islice(copy, 3):
            print repr(elem), 
        else:
            print '...',
        print '>'
    else:
        print repr(result)
    _ = result


# intended result
>>> enumerate('abcdefgh')
<enumerate object at 0x401a102c:  (0, 'a') (1, 'b') (2, 'c') ...>
>>> list(_)
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g'),
(7, 'h'), (8, 'i'), (9, 'j'), (10, 'k'), (11, 'l'), (12, 'm'), (13,
'n')]


Raymond Hettinger



More information about the Python-list mailing list