8
\$\begingroup\$

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.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Trying to insert this tuple into a dict or set crashes the python interpreter \$\endgroup\$
    – mousetail
    Sep 18, 2023 at 15:43
  • 1
    \$\begingroup\$ Edited to account for that; didn't know, thanks! \$\endgroup\$
    – Shuri2060
    Sep 18, 2023 at 16:11
  • 1
    \$\begingroup\$ May x be anything we choose? Or must it be able to be anything reasonable? \$\endgroup\$ Sep 18, 2023 at 18:39
  • 4
    \$\begingroup\$ I assume you don't want the loophole where x is an object for which __eq__ is defined so that it equals anything? \$\endgroup\$
    – xnor
    Sep 18, 2023 at 22:36
  • 9
    \$\begingroup\$ Why such probably doesn't exist: I think the problem lies with tuple.__hash__ which somewhat expectedly doesn't take kindly to the unbounded recursive structure. If you subclass tuple and reimplement __hash__ it looks like you can make it work: proof of concept. Not very golfy, though. \$\endgroup\$
    – loopy walt
    Sep 19, 2023 at 3:09

1 Answer 1

11
\$\begingroup\$

Python 3, 48 bytes

from marshal import*
t=loads(b'\xa9\1r\0\0\0\0')

Try it online!

Not portable, depends on the interpreter. There's probably a similar (portable) pickle version, although it would need to be handcrafted since the pickle.dumps gives an error due to recursion depth.

Uses marshal, which is a standard library for serializing Python objects. The string is the string received by serializing the object produced by the code in the question.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You may want to add , t == (t,) to your print in the TIO-link, since that's what the question is asking for. It does seem to result in True though, but I'd prefer an explanation before I upvote it tbh. \$\endgroup\$ Sep 19, 2023 at 7:25
  • 3
    \$\begingroup\$ @KevinCruijssen This SO answer has an explanation for an identical approach. \$\endgroup\$
    – att
    Sep 19, 2023 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.