Here's a simple version that copies the tree by feeding the tuple
constructor with a recursive generator. To test it, I've written another recursive function compare_trees
, which you can use to check that corresponding tuples aren't identical (using is
), and that corresponding inner items have the same value (using ==
).
def copy_tree(tree):
return tuple(copy_tree(u) if isinstance(u, tuple) else u for u in tree)
def compare_trees(tree1, tree2, indent=''):
print(indent, 'tree1', tree1, 'tree2', tree2, 'identical', tree1 is tree2)
indent += ' '
for u1, u2 in zip(tree1, tree2):
if isinstance(u1, tuple) and isinstance(u2, tuple):
compare_trees(u1, u2, indent)
else:
print(indent, 'item1', u1, 'item2', u2, 'equal', u1 == u2)
a = ((3, 2), 1, (4,), 5, (6, (7, 8)))
b = copy_tree(a)
print(b)
compare_trees(a, b)
output
((3, 2), 1, (4,), 5, (6, (7, 8)))
tree1 ((3, 2), 1, (4,), 5, (6, (7, 8))) tree2 ((3, 2), 1, (4,), 5, (6, (7, 8))) identical False
tree1 (3, 2) tree2 (3, 2) identical False
item1 3 item2 3 equal True
item1 2 item2 2 equal True
item1 1 item2 1 equal True
tree1 (4,) tree2 (4,) identical False
item1 4 item2 4 equal True
item1 5 item2 5 equal True
tree1 (6, (7, 8)) tree2 (6, (7, 8)) identical False
item1 6 item2 6 equal True
tree1 (7, 8) tree2 (7, 8) identical False
item1 7 item2 7 equal True
item1 8 item2 8 equal True
I suppose that it's a little ironic that the test code is larger and more complex than the code we want to test, but sometimes that's inevitable. ;)