Hashable object with self references OR how to create a tuple that refers to itself

Duncan Booth duncan.booth at invalid.invalid
Mon Jun 18 14:19:31 EDT 2012


Dieter Maurer <dieter at handshake.de> wrote:

> You can create a tuple in "C" and then put a reference to itself into
> it, but I am quite convinced that you cannot do it in Python itself.
> (Of course, you could use "cython" to generate C code with a source
> language very similar to Python).

I don't think you can even do it in C without breaking the specified API: 

PyTuple_SetItem fails if the reference count of the tuple is not 1, but it 
also steals the reference of the object that is being added to the tuple so 
if you don't increment the reference count before attempting to add the 
tuple to itself you've broken the rules for reference counting.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list