Returns a safe copy of a variable





10
Date Submitted Sun. Aug. 13th, 2006 10:38 PM
Revision 1 of 1
Scripter ASmith
Tags python | util
Comments 0 comments
Returns a safe copy if the value argument is mutable, otherwise returns the original argument. If the feedback keyword argument is True, then a tuple is returned containing the resulting argument, and whether or not is was mutable.

#Author:  Keven Dangoor
#Project: TurboGears
#File:  util.py

def copy_if_mutable(value, feedback=False):
  if isinstance(value, dict):
    mutable = True
    value = value.copy()
   elif isinstance(value, list):
     mutable = True
     value = value[:]
   else:
     mutable = False
   if feedback:
     return (value, mutable)
   else:
     return value
 

Comments

There are currently no comments for this snippet.

Voting