def _swap(obj, i, j):
    """Swaps the elements at index `i` and index `j` in `obj`.

    If `obj` has a ``copy()`` method, ``obj[j]`` gets a copy of ``obj[i]``.

    """
    temp = obj[i]
    if hasattr(temp, 'copy'):
        temp = temp.copy()
    obj[i] = obj[j]
    obj[j] = temp
    return obj
