generator object, next method

Duncan Booth duncan.booth at invalid.invalid
Thu Sep 8 04:10:15 EDT 2005


simonwittber at gmail.com wrote:

> 
>>>> gen = iterator()
>>>> gen.next
><method-wrapper object at 0x009D1B70>
>>>> gen.next
><method-wrapper object at 0x009D1BB0>
>>>> gen.next
><method-wrapper object at 0x009D1B70>
>>>> gen.next
><method-wrapper object at 0x009D1BB0>
>>>> gen.next is gen.next
> False
> 
> 
> What is behind this apparently strange behaviour? (The .next method
> seems to alternately bind to two different objects)

It is a combination of factors.

1) Every time you access gen.next you create a new method-wrapper object.
2) Typing an expression at the interactive prompt implicitly assigns the 
result of the expression to the variable '_'
3) When the method-wrapper is destroyed the memory becomes available to be 
reused the next time a method-wrapper (or other object of similar size) is 
created.

So in fact you have 6 different objects produced by your 6 accesses to 
gen.next although (since you never have more than 3 of them in existence at 
a time) there are probably only 3 different memory locations involved.



More information about the Python-list mailing list