I have been working in Python for a few months now, and it has occurred to me that I often overlook vocabulary that escapes me at first glance, instead trying to get the gist of an idea. Now, looking back, I still find myself confused beyond belief at what the term consume refers to. My initial interest came from explanations of iterators which spoke of a value of an iterator being consumed. However, looking around, this does not seem to be commonplace in the Python lexicon. Or is it? Digging around here finds mostly references to Web Services, and one or two discussions on how to hide this or that result of a function.
I suppose then, to break down my ignorance into a few base points:
- Does “consuming” do different things in different Pythonic contexts?
- What happens to data when it is consumed, such as in
iter()? - When a variable is assigned to an iterator’s result– the allegedly consumed piece of data– does it no longer belong to the iterator?
- Can you consume more than one value from an iterator object in a single call to the iterator?
I hope that makes some sort of sense. Note that this is not in reference to any particular need; I’m simply confused beyond rational plausibility.
EDIT: One more thing… does an iterated value (when called with next()) stay in memory?
In fact, we must distinguish two cases.
Remember what Greg Hewgill wrote:
First case:
the iterator calculates the object that it must produce when stimulated; that is to say, the produced object wasn’t existing before the call of
next(). Consequently, if a name is assigned to the object, this latter will survive; if not , the object will exist without being binded to a name in a namespace during a certain time, and then it will vanish in the memory, that is to say the bits it occupies will be used for another object later or sooner.The second case
is when the iterator returns formerly existing objects belonging to a list, a tuple, a dictionary, etc.. In this case, each object produced by a
next()had already a binding with a name. Then if the object is assigned to a new name when it “pops” out of the iterator, there will be two names binded to the object. And if the object is not assigned to a name, it will continue to be binded to only one name, what is sufficient to maintain the object alive.In common:
Each time an object is produced by a call of an iterator, if no name is assigned to him, the only result of the operation is that the iterator has been “consumed”. It’s a manner to say that even if there is no permanent consequence after the production of an object, it has happened something that let a trace inside the iterator.
One speaks of consuming the iterator when a name is assigned to the object, too, however, I don’t want to confuse.
Note:
In fact, in case of an object pre-existing in a list, say, it may be that it had no name. But the list holds a reference of every object it “contains”… In fact a list doesn’t “contains” objects, but only references to objects… Well that goes beyond what I wanted to say.
.
You should’nt write 3: “When a variable is assigned to …”
The word variable is a pitfall in Python because it has an ambiguous signification. There are no variables in Python, in the common sense known in other langages, that is to say a « delimited portion of memory whose value can change ». There are only objects. The word variable is habitually used to mean an identifier. So it is a better practice to call it identifier, or name. This avoids confusion.
.
I don’t think that it’s possible to obtain two returns from the iterator with only one call
next()