From: daryn on
I'm just playing around with the iter function and I realize that I
can use the iterator returned by it long after the original object has
any name bound to it. Example:

>>>a=[1,2,3,4]
>>>b=iter(a)
>>>b.next()
1
>>>a[1]=99
>>>a[3]=101
>>>del a
>>>b.next()
99
>>>b.next()
3
>>>b.next()
101

it seems as if the original object is never being garbage collected
even though there is no name bound to it. Does the name bound to the
iterator object count as a reference to the original object for
garbage collection purposes? Is there some way to retrieve/manipulate
the original object via the iterator?

Just trying to understand how this all works.

-thanks for any help you can give
daryn
From: Peter Otten on
daryn wrote:

> I'm just playing around with the iter function and I realize that I
> can use the iterator returned by it long after the original object has
> any name bound to it. Example:
>
>>>>a=[1,2,3,4]
>>>>b=iter(a)
>>>>b.next()
> 1
>>>>a[1]=99
>>>>a[3]=101
>>>>del a
>>>>b.next()
> 99
>>>>b.next()
> 3
>>>>b.next()
> 101
>
> it seems as if the original object is never being garbage collected
> even though there is no name bound to it. Does the name bound to the
> iterator object count as a reference to the original object for
> garbage collection purposes?

The listiterator object internally holds a reference to the list, but
doesn't make it available to Python code.

> Is there some way to retrieve/manipulate
> the original object via the iterator?

Not without dirty tricks (ctypes).

> Just trying to understand how this all works.

If you can read C look here:

http://svn.python.org/view/python/branches/py3k/Objects/listobject.c?revision=81032&view=markup

Peter
From: Terry Reedy on
On 8/9/2010 12:11 PM, daryn wrote:
> I'm just playing around with the iter function and I realize that I
> can use the iterator returned by it long after the original object has
> any name bound to it. Example:
>
>>>> a=[1,2,3,4]
>>>> b=iter(a)
>>>> b.next()
> 1
>>>> a[1]=99

Changing a list while iterating through it is possible, sometimes
useful, but error prone, especially with insert or delete. Changing a
dict while iterating through it is prohibited since the iteration order
depends on the exact internal structure. That in turn depends on the
history of additions and deletions.

>>>> a[3]=101
>>>> del a
>>>> b.next()
> 99
>>>> b.next()
> 3
>>>> b.next()
> 101
>
> it seems as if the original object is never being garbage collected
> even though there is no name bound to it.

The fact that CPython currently deletes some things immediately is a
current implementation detail, subject to change.

> Does the name bound to the
> iterator object count as a reference to the original object for
> garbage collection purposes?

Not quite. The iterator obviously has to have an internal reference,
which amount to the almost the same thing. However, if you put the
iterator in a list and deleted the name binding, both the iterator and
list are still kept around.

> Is there some way to retrieve/manipulate
> the original object via the iterator?

If you do dir(b), you will only see the standard __xx__ methods, most of
which are inherited. Iterators can be written to expose an underlying
object, if there is one, but some do not have one and this is not part
of the simple iterator protocol. Hence builtin iterators for builtins do
not do so.

If one actually needed such lookup, this wrapper should work.

class myiter():
def __init__(self, ob):
self.ob = ob
self.__itnext__ = iter(ob).__next__
def __iter__(self):
return self
def __next__(self):
return self.__itnext__()

it = myiter([1,2,3])
print (it.ob, list(it))

# [1, 2, 3] [1, 2, 3]

> Just trying to understand how this all works.

Keep experimenting. Python makes is so easy, especially with an edit
window such as with IDLE and a Run command. I initially tried not
defining myiter.__next__ and instead wrote:
self.__next__ = iter(ob).__next__
but that does not work because special __xx__ methods are typically
looked up on the class, not the instance. I know that but was not
completely sure about this case.

--
Terry Jan Reedy

From: Steven D'Aprano on
On Mon, 09 Aug 2010 09:11:37 -0700, daryn wrote:

> I'm just playing around with the iter function and I realize that I can
> use the iterator returned by it long after the original object has any
> name bound to it.

Yes, the same as everything else in Python. Iterators aren't unique here.

>>> a = [1,2,3]
>>> b = [None, a, None]
>>> id(a) == id(b[1]) # Same object, not a copy?
True
>>> del a
>>> print b
[None, [1, 2, 3], None]



> it seems as if the original object is never being garbage collected even
> though there is no name bound to it.

Of course not. That would be a Bad Thing if Python garbage collected an
object while other objects were still using it. Can you say "core dump"?



> Does the name bound to the
> iterator object count as a reference to the original object for garbage
> collection purposes?

No, but the iterator itself does.

The technique is called *reference* counting, not "name counting". Each
name is a reference, but not every reference is a name.


> Is there some way to retrieve/manipulate the
> original object via the iterator?

Depends on the iterator. For the standard iterator created by iter(), I
don't think so. But for a custom iterator type, there can be if you want:

class MyIter(object):
"""Quick and dirty iterator."""
def __init__(self, data):
self.data = data
self.i = 0
def __iter__(self):
return self
def next(self):
try:
o = self.data[self.i]
except IndexError:
raise StopIteration
self.i += 1
return o


>>> it = MyIter([1,2,3,4])
>>> it.next()
1
>>> del it.data[1:3]
>>> it.next()
4




--
Steven