Python dot-equals (syntax proposal)
Tim Chase
tim at thechases.com
Sat May 1 08:13:42 EDT 2010
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).
-tkc
More information about the Python-list
mailing list