Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread Steven D'Aprano
On 15/08/12 04:25, Matt Gregory wrote: 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? Only the person making the copy can answer that question, but in general,

Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread eryksun
On Tue, Aug 14, 2012 at 2:25 PM, Matt Gregory wrote: > > 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? C

Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread Matt Gregory
On 8/14/2012 5:28 AM, eryksun wrote: On Tue, Aug 14, 2012 at 6:01 AM, eryksun 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 co

Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread eryksun
On Tue, Aug 14, 2012 at 6:01 AM, eryksun 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_over

Re: [Tutor] overriding instance attributes with keywords

2012-08-14 Thread eryksun
On Mon, Aug 13, 2012 at 10:28 PM, Steven D'Aprano wrote: > >> def new_with_overrides(obj1, **kwds): >> obj2 = obj1.__new__(obj1.__class__) >> obj2.__dict__.update(obj1.__dict__) >> for k, v in kwds.items(): >> if k in obj2.__dict__: >> obj2.__dict__[k] = v >>

Re: [Tutor] overriding instance attributes with keywords

2012-08-13 Thread Steven D'Aprano
On 14/08/12 07:16, eryksun wrote: On Mon, Aug 13, 2012 at 3:13 PM, Gregory, Matthew wrote: I'm trying to create a new instance from an existing instance def new_with_overrides(s1, **kwargs): new_params = {'a': s1.a, 'b': s1.b} for (k, v) in kwargs.iteritems(): if k in ne

Re: [Tutor] overriding instance attributes with keywords

2012-08-13 Thread eryksun
On Mon, Aug 13, 2012 at 3:13 PM, Gregory, Matthew wrote: > > I'm trying to create a new instance from an existing instance > def new_with_overrides(s1, **kwargs): > new_params = {'a': s1.a, 'b': s1.b} > for (k, v) in kwargs.iteritems(): > if k in new_params: > new_para