inconsistency with += between different types ?

Donn Cave donn at u.washington.edu
Mon Aug 5 12:32:09 EDT 2002


Quoth Andreas.Leitgeb at siemens.at (Andreas Leitgeb):
| The following snippet demonstrates it:
| --- snip ---
| def f(x,y): x+=y
|
| d='foo';     f(d,'bar');   print d    #  ->  'foo'
| i=4;         f(i,2);       print i    #  ->  4
| #but:
| l=[1,2,3];   f(l,[7,8,9]); print l    #  ->  [1, 2, 3, 7, 8, 9]
| --- snip ---
|  
| It seems that  lists are the only type, where the changes get visible
| outside, although I'd have expected all types to behave as lists do.
|
| I've not tried other extended-assignment(*)-operators *=,-=,...=, yet.
|
| The question seems to be:
|   Is an extended assignment(*) a "modification" of an object or
|   a re-assignment to the left-side operand ?

Yes (either can be true, though of course not at the same time.)

| Is it just a question of whether and how __iadd__ is implemented,

Yes.

| or is there some deeper meaning in this ?
| Is it a bug ?  

No, just an ill-conceived feature.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list