UniCoreFW

deep_copy()

Create a deep copy of the given object without using imports.

Implementation

Args: obj: The object to copy Returns: A deep copy of the object

Example

deep_copy({"a": 1, "b": 2})

Expected output: {"a": 1, "b": 2}

Source Code

def deep_copy(obj: Any) -> Any: if isinstance(obj, dict): return {k: deep_copy(v) for k, v in obj.items()} elif isinstance(obj, list): return [deep_copy(elem) for elem in obj] elif isinstance(obj, tuple): return tuple(deep_copy(elem) for elem in obj) elif isinstance(obj, str): # Strings are immutable, return as is return obj else: # For immutable objects like int, float, return as is return obj