What does it mean to “consume” in Python? In an iterator?

前端 未结 6 742
既然无缘
既然无缘 2020-12-11 01:45

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 gis

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 02:14

    Concerning 2.

    In fact, we must distinguish two cases.

    Remember what Greg Hewgill wrote:

    An "iterator" is a single object that is responsible for creating some sequence of elements. This sequence might be elements of an existing list, or it might be something calculated, like prime numbers or the decimal digits of π.

    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.

    .

    Concerning 3

    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.

    .

    Concerning 4

    I don't think that it's possible to obtain two returns from the iterator with only one call next()

提交回复
热议问题