Semi-newbie, rolling my own __deepcopy__

Michael Spencer mahs at telcopartners.com
Mon Apr 4 15:04:42 EDT 2005


Steven Bethard wrote:
> Michael Spencer wrote:
> 
>>     def __deepcopy__(self, memo={}):
>>         from copy import deepcopy
>>         result = self.__class__()
>>         memo[id(self)] = result
>>         result.__init__(deepcopy(tuple(self), memo))
>>         return result
> 
> 
> I know this is not your recipe, but is there any reason to use
>     self.__class__()
> instead of
>     type(self)()
> if you know you're inside a new-style class?
> 
> STeVe
I don't know - aren't they identical? I would write self.__class__ (without 
claiming that that's better)

BTW, I had a different question about the method:

wouldn't:
     result = self.__class__.__new__()
or in your form:
     result = type(self).__new__()

be better (i.e., clearer and possibly safer) than calling __init__ twice (but I 
haven't tried it!)

Michael






More information about the Python-list mailing list