TypeError expected in an augmented assignment

candide c.candide at laposte.net
Thu Jul 3 05:35:01 EDT 2014



> >From that link:
> 
> 
> 
> """
> 
> An augmented assignment expression like x += 1 can be rewritten as x =
> 
> x + 1 to achieve a similar, but not exactly equal effect. In the
> 
> augmented version, x is only evaluated once. Also, when possible, the
> 
> actual operation is performed in-place, meaning that rather than
> 
> creating a new object and assigning that to the target, the old object
> 
> is modified instead.
> 
> """
> 
> 
> 
> The significance here is that the augmented assignment may not
> 
> necessarily be at all comparable to the non-augmented version, but
> 
> ought to have *approximately* the same *intention*. 


This is not my reading. 




> 
> of situations where the two will differ, eg when there are multiple
> 
> references to the same object:
> 
> 
> 
> >>> a = b = [1,2]
> 
> >>> a += [3]
> 
> >>> a,b
> 
> ([1, 2, 3], [1, 2, 3])
> 
> >>> a = a + [4]
> 
> >>> a,b
> 
> ([1, 2, 3, 4], [1, 2, 3])
> 
> 

OK but this behavior is in conformance with the Reference Manual (cf. your quote above : "when possible, the actual operation is performed in-place"). This is not my point because the doc explictly claims that "an augmented assignment [...] performs the binary operation specific to the type of assignment on the two operands".



More information about the Python-list mailing list