"""用嵌套容器验证别名、浅拷贝、深拷贝和参数绑定。"""
from copy import deepcopy


def rebind(values):
    values = ["local"]  # 只改变函数内部名称。
    return values


def main():
    original = {"tags": ["python"]}
    alias = original
    shallow = original.copy()
    deep = deepcopy(original)
    shallow["tags"].append("backend")
    assert alias is original
    assert shallow is not original
    assert shallow["tags"] is original["tags"]
    assert original["tags"] == ["python", "backend"]
    assert deep["tags"] == ["python"]
    values = ["caller"]
    assert rebind(values) == ["local"]
    assert values == ["caller"]
    bad = [[]] * 2  # 两个位置引用同一个列表。
    bad[0].append(1)
    good = [[] for _ in range(2)]
    good[0].append(1)
    assert bad == [[1], [1]] and good == [[1], []]
    # memo（已复制对象表）保留共享拓扑，也终止循环递归。
    child = ["shared"]
    graph = [child, child]
    graph.append(graph)
    cloned = deepcopy(graph)
    assert cloned is not graph
    assert cloned[0] is cloned[1] and cloned[0] is not child
    assert cloned[2] is cloned
    print("memo sharing/cycle:", cloned[0] is cloned[1], cloned[2] is cloned)
    print("original:", original)
    print("deep:", deep)
    print("rows:", bad, good)


if __name__ == "__main__":
    main()
