2
改變參數當從基類繼承,實現__deepcopy__
和繼承類改變論據__init__
,如何從基類的__deepcopy__
可以在繼承類重用?__deepcopy__和繼承與__init__
下面的例子:
class A(object):
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
def __deepcopy__(self, memo):
newone = type(self)(self.arg1, self.arg2)
...
class B(A):
def __init__(self, arg1):
A.__init__(self, arg1, None)
def __deepcopy__(self, memo):
newone = A.__deepcopy__(self, memo) # fails, because __deepcopy__ of
# A tries to create an instance of
# B with to many arguments
...
謝謝,這對我來說工作得很好。 – Jester