copy on write

88888 Dihedral dihedral88888 at googlemail.com
Thu Feb 2 14:33:17 CET 2012


在 2012年1月14日星期六UTC+8上午6时48分29秒,Evan Driscoll写道:
> On 01/13/2012 03:20 PM, Neil Cerutti wrote:
> > They perform the same action, but their semantics are different.
> > operator+ will always return a new object, thanks to its
> > signature, and operator+= shall never do so. That's the main
> > difference I was getting at.
> 
> I was talking about the combination of + and =, since the discussion is 
> about 'a = a + b' vs 'a += b', not 'a + b' vs 'a += b' (where the 
> differences are obvious).
> 
> And I stand by my statement. In 'a = a + b', operator+ obviously returns 
> a new object, but operator= should then go and assign the result to and 
> return a reference to 'a', just like how 'a += b' will return a 
> reference to 'a'.
>

The operation a+b means add(a,b) and returns a result instance, furthermore a and b can't be modified.

The expression a = a+b are two operations not one. But in C or C++ the  problem is mixing operations and expressions in a free style allowed.
 
The operation a+=b means a modified by b and b can't be changed.
Note that no new instance is necessary in a+=b.
 
 

> If you're working in C++ and overload your operators so that 'a += b' 
> and 'a = a + b' have different observable behaviors (besides perhaps 
> time), then either your implementation is buggy or your design is very 
> bad-mannered.
> 
> Evan

Do you mean the result instances after 'a+=b' and 'a=a+b' or 
the actions of behaviors of instances involved  in performing 'a+=b' and 'a=a+b'?



More information about the Python-list mailing list