[Python-Dev] implementation of copy standard lib

Ron Adam rrr at ronadam.com
Wed Aug 17 13:59:34 EDT 2005


Michael Hudson wrote:
> Simon Brunning <simon.brunning at gmail.com> writes:
> 
> 
>>I think that copy is very rarely used. I don't think I've ever imported it.
>>
>>Or is it just me?
> 
> 
> Not really.  I've used it once that I can recall, to copy a kind of
> generic "default value", something like:
> 
> def value(self, v, default):
>     if hasattr(source, v): return getattr(source, v)
>     else: return copy.copy(default)
> 
> (except not quite, there would probably be better ways to write
> exactly that).
> 
> Cheers,
> mwh

My most recent use of copy.deepcopy() was to save the state of a 
recusivly built object so that it could be restored before returning a 
result that involved making nested changes (with multiple methods) to 
the ojbects subparts as part of the result calculation.

The alternative would be to use a flag and shallow copies in all the 
methods that altered the object.  copy.deepcopy() was a lot easier as 
it's only needed in the method that initiates the result calculation.

Cheers,
Ron



More information about the Python-list mailing list