Goal: create a self-referencing tuple satisfying x == (x,)
in any version of Python. Standard libraries, matplotlib, numpy, pandas only.
Answers meeting this extra condition are prioritised (which may be impossible): this tuple should also be insertable into a dict or set.
If the extra condition is impossible, apart from the low-hanging optimization of the below code, I am still interested in a qualitatively different approach of similar length or better.
I had previously assumed this answer to be one of the shortest, but as @mousetail brought up, actually it crashes python when inserted into a dict or set.
import ctypes
tup = (0,)
ctypes.c_longlong.from_address(id(tup)+24).value = id(tup)
I notice this also happens with the other answer in that post, so I'm wondering if there's a way to resolve it (the ideal answer), or if that is inherent in python.
x
be anything we choose? Or must it be able to be anything reasonable? \$\endgroup\$x
is an object for which__eq__
is defined so that it equals anything? \$\endgroup\$tuple.__hash__
which somewhat expectedly doesn't take kindly to the unbounded recursive structure. If you subclasstuple
and reimplement__hash__
it looks like you can make it work: proof of concept. Not very golfy, though. \$\endgroup\$