On 8/14/2012 5:28 AM, eryksun wrote:
On Tue, Aug 14, 2012 at 6:01 AM, eryksun <eryk...@gmail.com> wrote:

Right, I overlooked classes with __slots__ and that override __new__
and other special methods. copy() is the better and more customizable
solution.

If copying is good enough, then this should work:

from copy import copy

def copy_with_overrides(obj1, **kwds):
     obj2 = copy(obj1)
     for k, v in kwds.items():
         if hasattr(obj2, k):
             setattr(obj2, k, v)
     return obj2

Thanks to you both for really helpful advice. A quick follow-up question - would you want to create a deepcopy of obj1 in the above example if your obj1 contained other objects? I think that Steven's class method gets around this?

thanks, matt

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to