Enumerate object is destroyed by casting?

Jeffrey Froman Jeffrey at Fro.man
Wed Aug 25 16:27:24 EDT 2004


Nick Jacobson wrote:

> Casting an 'enumerate' object destroys it??  Is that supposed to
> happen, or is it a bug?
> 
> 
> For example:
> a = ['a', 'b', 'c']
> e = enumerate(a)
> print dict(e)
> print dict(e)
> 
> 
> Result:
> {0: 'a', 1: 'b', 2: 'c'}
> {}

This is supposed to happen. Enumerate objects are essentially generators --
casting the object doesn't "destroy" it; but iterating over its values
"uses them up". In other words, after the first dict() call, your
enumerator is now empty. The same thing would happen if you simply iterated
over the object, like:

for thing in e:
 print thing





More information about the Python-list mailing list