实现 copy.deepcopy() 克隆函数
Implementing a copy.deepcopy() clone function
仅用于教育目的,我正在尝试实现copy.deepcopy()
的克隆功能。
在玩代码和谷歌搜索之后,我想到了以下函数:
def my_deepcopy(data):
if isinstance(data, dict):
result = {}
for key, value in data.items():
result[key] = my_deepcopy(value)
assert id(result) != id(data)
elif isinstance(data, list):
result = []
for item in data:
result.append(my_deepcopy(item))
assert id(result) != id(data)
elif isinstance(data, tuple):
aux = []
for item in data:
aux.append(my_deepcopy(item))
result = tuple(aux)
assert id(result) != id(data)
elif isinstance(data, (int, float, type(None), str, bool)):
result = data
else:
raise ValueError("unexpected type")
return result
似乎 可以使用所有 Python 基本类型及其组合:
# Various object types
lst_obj = [ 0, 1.1, 'foo', 'bar' ]
dict_obj = { 'zero' : 0, 'pi' : 3.1415, 'desc' : 'foobar' }
list_list_obj = [ [1,2,3], [4,5,6], [7,8,9] ]
tuple_list_obj = [ (-1,-1), (0,-1,0), (-1,0), (0,0,0,0) ]
dict_list_obj = [ {'zero' : 0}, {'pi' : 3.1415}, {'desc' : 'foobar'} ]
# Testing
my_deepcopy( lst_obj ) #OK!
my_deepcopy( dict_obj ) #OK!
my_deepcopy( list_list_obj ) #OK!
my_deepcopy( tuple_list_obj ) #OK!
my_deepcopy( dict_list_obj ) #OK!
到目前为止,还不错,但是 Arbitrary Types
呢?如何复制任意对象的实例?我如何检测它?任意类型是否有任何类型的复制构造函数?
为了使以下代码正常工作,我的函数中缺少什么:
class Xpto:
pass
arbitrary = [ Xpto(), Xpto() ]
my_deepcopy( arbitrary ) #ValueError("unexpected type")
您可以"Category Checking"做一些事情来更好地设计您的函数,通过任意对象的 __dict__
属性搜索序列或映射:
>>> import collections
>>> isinstance([], collections.Sequence)
True
>>> isinstance((), collections.Sequence)
True
>>> isinstance('foo', collections.Sequence)
True
>>> isinstance({}, collections.Mapping)
True
>>> isinstance(23, collections.Sequence)
False
>>> isinstance(None, collections.Sequence)
False
还要检查 __class__
或 __name__
属性,以检查对象是否是从某些 class 类型实例化的,该类型不在您的 if 语句中,例如:
def my_deepcopy(data):
if isinstance(data, dict):
result = {}
for key, value in data.items():
result[key] = my_deepcopy(value)
assert id(result) != id(data)
elif isinstance(data, list):
result = []
for item in data:
result.append(my_deepcopy(item))
assert id(result) != id(data)
elif isinstance(data, tuple):
aux = []
for item in data:
aux.append(my_deepcopy(item))
result = tuple(aux)
assert id(result) != id(data)
elif isinstance(data, (int, float, type(None), str, bool)):
result = data
elif hasattr(data, '__name__'):#Its a function or a class
auxObj = data.__class__() #Assuming it has not __init__ method defined
auxDict = {}
for key, value in data.__dict__:
auxDict[key] = my_deepcopy(value)
assert id(result) != id(data)
else:
raise ValueError("unexpected type")
return result
仅用于教育目的,我正在尝试实现copy.deepcopy()
的克隆功能。
在玩代码和谷歌搜索之后,我想到了以下函数:
def my_deepcopy(data):
if isinstance(data, dict):
result = {}
for key, value in data.items():
result[key] = my_deepcopy(value)
assert id(result) != id(data)
elif isinstance(data, list):
result = []
for item in data:
result.append(my_deepcopy(item))
assert id(result) != id(data)
elif isinstance(data, tuple):
aux = []
for item in data:
aux.append(my_deepcopy(item))
result = tuple(aux)
assert id(result) != id(data)
elif isinstance(data, (int, float, type(None), str, bool)):
result = data
else:
raise ValueError("unexpected type")
return result
似乎 可以使用所有 Python 基本类型及其组合:
# Various object types
lst_obj = [ 0, 1.1, 'foo', 'bar' ]
dict_obj = { 'zero' : 0, 'pi' : 3.1415, 'desc' : 'foobar' }
list_list_obj = [ [1,2,3], [4,5,6], [7,8,9] ]
tuple_list_obj = [ (-1,-1), (0,-1,0), (-1,0), (0,0,0,0) ]
dict_list_obj = [ {'zero' : 0}, {'pi' : 3.1415}, {'desc' : 'foobar'} ]
# Testing
my_deepcopy( lst_obj ) #OK!
my_deepcopy( dict_obj ) #OK!
my_deepcopy( list_list_obj ) #OK!
my_deepcopy( tuple_list_obj ) #OK!
my_deepcopy( dict_list_obj ) #OK!
到目前为止,还不错,但是 Arbitrary Types
呢?如何复制任意对象的实例?我如何检测它?任意类型是否有任何类型的复制构造函数?
为了使以下代码正常工作,我的函数中缺少什么:
class Xpto:
pass
arbitrary = [ Xpto(), Xpto() ]
my_deepcopy( arbitrary ) #ValueError("unexpected type")
您可以"Category Checking"做一些事情来更好地设计您的函数,通过任意对象的 __dict__
属性搜索序列或映射:
>>> import collections
>>> isinstance([], collections.Sequence)
True
>>> isinstance((), collections.Sequence)
True
>>> isinstance('foo', collections.Sequence)
True
>>> isinstance({}, collections.Mapping)
True
>>> isinstance(23, collections.Sequence)
False
>>> isinstance(None, collections.Sequence)
False
还要检查 __class__
或 __name__
属性,以检查对象是否是从某些 class 类型实例化的,该类型不在您的 if 语句中,例如:
def my_deepcopy(data):
if isinstance(data, dict):
result = {}
for key, value in data.items():
result[key] = my_deepcopy(value)
assert id(result) != id(data)
elif isinstance(data, list):
result = []
for item in data:
result.append(my_deepcopy(item))
assert id(result) != id(data)
elif isinstance(data, tuple):
aux = []
for item in data:
aux.append(my_deepcopy(item))
result = tuple(aux)
assert id(result) != id(data)
elif isinstance(data, (int, float, type(None), str, bool)):
result = data
elif hasattr(data, '__name__'):#Its a function or a class
auxObj = data.__class__() #Assuming it has not __init__ method defined
auxDict = {}
for key, value in data.__dict__:
auxDict[key] = my_deepcopy(value)
assert id(result) != id(data)
else:
raise ValueError("unexpected type")
return result