This isn't so much an idea for Python, as a request for ideas to solve a
problem in Python.
Back in the early days of Python, printing recursive lists could crash
the interpreter:
a = [1, 2, 3]
a.append(a)
print(a)
If you try that today, you get a nice display:
[1, 2, 3, [...]]
It's not just lists that we can have this. Dicts and nearly any object
can too:
d = {}
d[None] = d
class Demo:
pass
x = Demo()
x.attr = x
With a bit of jiggery-pokery, even tuples can be recursive:
t = (1, 2, 3, [])
t[3].append(t)
The built-ins handle these cases okay. Likewise the copy.deepcopy
function takes care to avoid getting stuck in a loop with recursive data
structures.
On the other hand, when I write code to process nested data structures,
I always worry about such recursive patterns, but actually doing
something about it is too hard. So I just ignore the risk and feel
guilty about it. I'm sure I'm not the only one who feels guilty about
ignoring this problem.
To say nothing of those who don't even know there's a problem to worry
about.
Is there something we can do to make it easier for people to deal with
recursive data structures, like the list repr and deepcopy code does? Is
there a pattern we can abstract out and provide as a tool or recipe for
people to use in their own code?
Relevant: https://bugs.python.org/issue42801
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ZHDVXD7FN5XLL4DP7QTIOID7FMG7Q5IX/
Code of Conduct: http://python.org/psf/codeofconduct/