問題描述
我已經在 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:
- 消費"在不同的 Python 語境中會做不同的事情嗎?
- 數據在被消費時會發生什么,例如在
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 中?在迭代器中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!