Tim Chase, 01.05.2010 14:13:
On 05/01/2010 12:08 AM, Patrick Maupin wrote:
+=, -=, /=, *=, etc. conceptually (and, if lhs object supports in-
place operator methods, actually) *modify* the lhs object.

Your proposed .= syntax conceptually *replaces* the lhs object
(actually, rebinds the lhs symbol to the new object).

The += family of operators really do rebind the symbol, not modify the
object.

 >>> from decimal import Decimal
 >>> d = Decimal(42)
 >>> e = Decimal(18)
 >>> orig = d
 >>> d += e
 >>> d
Decimal("60")
 >>> e
Decimal("18")
 >>> orig
Decimal("42")
 >>> d is orig
False

If your suggestion that += *modifies* the object, then orig would now
unintuitively contain 60 and "d is orig" would return True.

This doesn't preclude you from implementing a self-mutating += style
__add__ method and returning "self", but it's usually a bad idea unless
it's dire for performance (and even then, think it over a couple times).

It's not like this was unprecedented in Python, though:

  Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
  [GCC 4.4.3] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> l = [1,2,3]
  >>> a = l
  >>> l += [4,5,6]
  >>> l
  [1, 2, 3, 4, 5, 6]
  >>> a
  [1, 2, 3, 4, 5, 6]

And I'm pretty sure this wasn't just done for performance reasons. Mutable data types behave that way.

Stefan

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to