我已经在 Python 中工作了几个月,我突然想到,我经常忽略乍看之下无法理解的词汇,而是试图了解一个想法的要点.现在回想起来,我仍然对consume这个词的含义感到困惑.我最初的兴趣来自对迭代器的解释,它谈到了正在消耗的迭代器的值.但是,环顾四周,这在 Python 词典中似乎并不常见.或者是吗?在这里挖掘发现主要是对 Web 服务的引用,以及关于如何隐藏函数的这个或那个结果的一两个讨论.
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:
iter()
中?我希望这有点道理.请注意,这不是针对任何特定需求;我只是感到困惑,超出了合理的合理性.
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.
还有一件事......迭代值(当使用 next()
调用时)是否保留在内存中?
One more thing... does an iterated value (when called with next()
) stay in memory?
关于 2.
事实上,我们必须区分两种情况.
In fact, we must distinguish two cases.
记住 Greg Hewgill 写的:
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 π.
第一种情况:
迭代器计算受刺激时它必须产生的对象;也就是说,在调用 next()
之前,生成的对象并不存在.因此,如果为对象分配了名称,则后者将继续存在;如果不是,则该对象会在一定时间内不与命名空间中的名称绑定而存在,然后它将在内存中消失,也就是说它所占用的位将在以后或早日用于另一个对象.
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.
第二种情况
是当迭代器返回以前存在的属于列表、元组、字典等的对象时.在这种情况下,由 next()
生成的每个对象已经与名称.然后,如果对象在弹出"迭代器时被分配了一个新名称,那么将有两个名称绑定到该对象.如果对象没有被分配一个名字,它会继续只绑定一个名字,这足以维持对象的存活.
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.
共同点:
每次调用迭代器生成对象时,如果没有为其分配名称,则操作的唯一结果是迭代器已被消费".这是一种说法,即使在生成对象后没有永久性后果,它也发生了一些在迭代器内部留下痕迹的事情.
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.
注意:
事实上,如果一个对象预先存在于一个列表中,比如说,它可能没有名字.但是列表包含了它包含"的每个对象的引用……事实上,列表并不包含"对象,而只是对对象的引用……这超出了我想说的范围.
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.
.
关于3
你不应该写3:当一个变量被赋值给..."
变量这个词在 Python 中是一个陷阱,因为它的含义不明确.Python 中没有变量,在其他语言中的常识中,即 值可以改变的内存的分隔部分.只有对象.变量这个词习惯性地用来表示一个标识符.因此,最好将其称为 identifier 或 name.这样可以避免混淆.
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.
.
关于4
我认为只有一次调用 next()
这篇关于“消费"是什么意思?在 Python 中?在迭代器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!