Enumerate object is destroyed by casting?

Shalabh Chaturvedi shalabh at cafepy.com
Wed Aug 25 17:25:19 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 not casting (there is no casting in Python). It is creating a 
new dict from an iterable. enumerate(a) creates an iterable that runs 
over the items *once*. After that there is nothing left to return.

HTH,

Shalabh




More information about the Python-list mailing list