set and dict iteration

MRAB python at mrabarnett.plus.com
Sat Aug 18 18:14:05 EDT 2012


On 18/08/2012 21:29, Aaron Brady wrote:
> On Friday, August 17, 2012 4:57:41 PM UTC-5, Chris Angelico wrote:
>> On Sat, Aug 18, 2012 at 4:37 AM, Aaron Brady <castironpi at gmail.com> wrote:
>>
>> > Is there a problem with hacking on the Beta?
>>
>>
>>
>> Nope. Hack on the beta, then when the release arrives, rebase your
>>
>> work onto it. I doubt that anything of this nature will be changed
>>
>> between now and then.
>>
>>
>>
>> ChrisA
>
> Thanks Chris, your post was encouraging.
>
> I have a question about involving the 'tp_clear' field of the types.
>
> http://docs.python.org/dev/c-api/typeobj.html#PyTypeObject.tp_clear
>
> '''
> ...The tuple type does not implement a tp_clear function, because it’s possible to prove that no reference cycle can be composed entirely of tuples.
> '''
>
> I didn't follow the reasoning in the proof; the premise is necessary but IMHO not obviously sufficient.  Nevertheless, the earlier diagram contains an overt homogeneous reference cycle.
>
> Reposting: http://home.comcast.net/~castironpi-misc/clpy-0062%20set%20iterators.png
>
> In my estimate, the 'tp_traverse' and 'tp_clear' fields of the set doesn't need to visit the auxiliary collection; the same fields of the iterators don't need to visit the primary set or other iterators; and references in the linked list don't need to be included in the iterators' reference counts.
>
> Can someone who is more familiar with the cycle detector and cycle breaker, help prove or disprove the above?
>
In simple terms, when you create an immutable object it can contain
only references to pre-existing objects, but in order to create a cycle
you need to make an object refer to another which is created later, so
it's not possible to create a cycle out of immutable objects.

However, using Python's C API it _is_ possible to create such a cycle,
by mutating an otherwise-immutable tuple (see PyTuple_SetItem and
PyTuple_SET_ITEM).




More information about the Python-list mailing list