473,486 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Enumerate object is destroyed by casting?

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'}
{}
Jul 18 '05 #1
2 2036
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
Jul 18 '05 #2
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
9202
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
5
3724
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
12
3252
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
8
2924
by: pt | last post by:
Hallo, i wonder how it is going to be of this code below regarding of the return of temporary object. Prototypes: =========== bool Activation(TCHAR *c); std::basic_string<TCHAR> GetFile();
16
25390
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
2
2183
by: Jeff | last post by:
Hello, I assigned a new object to a local variable ("req") in a function (see below). The local variable "req" is obviously destroyed when the function exits, but should the object referenced by...
4
1956
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
21
2292
by: James Stroud | last post by:
I think that it would be handy for enumerate to behave as such: def enumerate(itrbl, start=0, step=1): i = start for it in itrbl: yield (i, it) i += step This allows much more flexibility...
10
2550
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.