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

Dieter Maurer dieter at handshake.de
Fri Jun 15 20:22:33 CEST 2012

"Edward C. Jones" <edcjones at comcast.net> writes:

> I am trying to create a collection of hashable objects, where each
> object contains references to
> other objects in the collection.  The references may be circular.
> To simplify, one can define
>     x= list()
>     x.append(x)
> which satisfies x == [x].
> Can I create a similar object for tuples which satisfies x == (x,)?

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).

But, you do not need tuples; You could use a standard class:

>>> class C(object): pass
>>> c=C()
>>> c.c=c
>>> d=dict(c=c)
>>> d
{'c': <__main__.C object at 0xb737f86c>}


More information about the Python-list mailing list