問(wèn)題描述
我已經(jīng)在 Python 中工作了幾個(gè)月,我突然想到,我經(jīng)常忽略乍看之下無(wú)法理解的詞匯,而是試圖了解一個(gè)想法的要點(diǎn).現(xiàn)在回想起來(lái),我仍然對(duì)consume這個(gè)詞的含義感到困惑.我最初的興趣來(lái)自對(duì)迭代器的解釋,它談到了正在消耗的迭代器的值.但是,環(huán)顧四周,這在 Python 詞典中似乎并不常見.或者是嗎?在這里挖掘發(fā)現(xiàn)主要是對(duì) Web 服務(wù)的引用,以及關(guān)于如何隱藏函數(shù)的這個(gè)或那個(gè)結(jié)果的一兩個(gè)討論.
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.
我想,把我的無(wú)知分解成幾個(gè)基點(diǎn):
I suppose then, to break down my ignorance into a few base points:
- 消費(fèi)"在不同的 Python 語(yǔ)境中會(huì)做不同的事情嗎?
- 數(shù)據(jù)在被消費(fèi)時(shí)會(huì)發(fā)生什么,例如在
iter()
中? - 當(dāng)一個(gè)變量被分配給一個(gè)迭代器的結(jié)果(據(jù)稱是消耗的數(shù)據(jù))時(shí),它是否不再屬于迭代器?
- 您能否在一次調(diào)用迭代器時(shí)從迭代器對(duì)象中使用多個(gè)值?
我希望這有點(diǎn)道理.請(qǐng)注意,這不是針對(duì)任何特定需求;我只是感到困惑,超出了合理的合理性.
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.
還有一件事......迭代值(當(dāng)使用 next()
調(diào)用時(shí))是否保留在內(nèi)存中?
One more thing... does an iterated value (when called with next()
) stay in memory?
推薦答案
關(guān)于 2.
事實(shí)上,我們必須區(qū)分兩種情況.
In fact, we must distinguish two cases.
記住 Greg Hewgill 寫的:
Remember what Greg Hewgill wrote:
迭代器"是負(fù)責(zé)的單個(gè)對(duì)象用于創(chuàng)建一些元素序列.這個(gè)序列可能是現(xiàn)有列表的元素,或者它可能是計(jì)算出來(lái)的,比如素?cái)?shù)或π的小數(shù)位.
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 π.
第一種情況:
迭代器計(jì)算受刺激時(shí)它必須產(chǎn)生的對(duì)象;也就是說(shuō),在調(diào)用 next()
之前,生成的對(duì)象并不存在.因此,如果為對(duì)象分配了名稱,則后者將繼續(xù)存在;如果不是,則該對(duì)象會(huì)在一定時(shí)間內(nèi)不與命名空間中的名稱綁定而存在,然后它將在內(nèi)存中消失,也就是說(shuō)它所占用的位將在以后或早日用于另一個(gè)對(duì)象.
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.
第二種情況
是當(dāng)?shù)鞣祷匾郧按嬖诘膶儆诹斜怼⒃M、字典等的對(duì)象時(shí).在這種情況下,由 next()
生成的每個(gè)對(duì)象已經(jīng)與名稱.然后,如果對(duì)象在彈出"迭代器時(shí)被分配了一個(gè)新名稱,那么將有兩個(gè)名稱綁定到該對(duì)象.如果對(duì)象沒(méi)有被分配一個(gè)名字,它會(huì)繼續(xù)只綁定一個(gè)名字,這足以維持對(duì)象的存活.
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.
共同點(diǎn):
每次調(diào)用迭代器生成對(duì)象時(shí),如果沒(méi)有為其分配名稱,則操作的唯一結(jié)果是迭代器已被消費(fèi)".這是一種說(shuō)法,即使在生成對(duì)象后沒(méi)有永久性后果,它也發(fā)生了一些在迭代器內(nèi)部留下痕跡的事情.
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.
也有人說(shuō)在為對(duì)象分配名稱時(shí)使用迭代器,但是,我不想混淆.
One speaks of consuming the iterator when a name is assigned to the object, too, however, I don't want to confuse.
注意:
事實(shí)上,如果一個(gè)對(duì)象預(yù)先存在于一個(gè)列表中,比如說(shuō),它可能沒(méi)有名字.但是列表包含了它包含"的每個(gè)對(duì)象的引用……事實(shí)上,列表并不包含"對(duì)象,而只是對(duì)對(duì)象的引用……這超出了我想說(shuō)的范圍.
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.
.
關(guān)于3
你不應(yīng)該寫3:當(dāng)一個(gè)變量被賦值給..."
變量這個(gè)詞在 Python 中是一個(gè)陷阱,因?yàn)樗暮x不明確.Python 中沒(méi)有變量,在其他語(yǔ)言中的常識(shí)中,即 ? 值可以改變的內(nèi)存的分隔部分?.只有對(duì)象.變量這個(gè)詞習(xí)慣性地用來(lái)表示一個(gè)標(biāo)識(shí)符.因此,最好將其稱為 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.
.
關(guān)于4
我認(rèn)為只有一次調(diào)用 next()
這篇關(guān)于“消費(fèi)"是什么意思?在 Python 中?在迭代器中?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!