Augmented Assignement (was: Re: PEP scepticism)
donn at drizzle.com
Fri Jun 29 18:40:31 CEST 2001
Quoth "Alex Martelli" <aleaxit at yahoo.com>:
| "Bernhard Herzog" <bh at intevation.de> wrote in message
| news:6qn16r7i69.fsf at abnoba.intevation.de...
|> I think the main problem (in as much as there actually is a problem) is
|> that whether augmented assignment rebinds the left hand side depends on
|> the type of object, i.e. whether the object implements the appropriate
| Right! I think the main advantage (in as much as there actually is an
| advantage) is that whether augmented assignment rebids the left hand
| side depends on the type of object. i.e. whether the object implements
| the appropriate methods. This is called *polymorphism* and is a concept
| of *HUGE* power.
|> methods, which is very different from normal assignment which always
| Do you consider:
| fee.fie = foo
| fee[fie] = foo
| examples of "normal assignment"? Neither "always rebinds" -- it all
| depends on the type of object, i.e. whether the object (fee) implements
| the appropriate methods (__setattr__, __setitem__). Augmented
| assignment, like attribute assignment and item assignment, is
| polymorphic. Plain assignment to an _unqualified_ LHS isn't, so
| maybe *THAT* one is the "odd man out"?-)
That sure isn't the way I see it.
In an expression like x.a = b or x[i] = b, I see that the assignment
is indeed a function of x's setattr or setitem, but it's an operation
that clearly modifies x. In the present case, we're talking about an
operation that may modify x ... or not, in which case it modifies the
assignment namespace*. That doesn't sound like polymorphism of x as
much as a language feature that varies the semantics of the operation
depending on which choice of semantics is convenient for the object.
Maybe the convenience is worth it, but the point was well taken, this
has the potential to enhance confusion.
self.a += b, where a is a class attribute, doesn't replace the
class attribute but rather adds an instance attribute. That's
consistent, just defining my term "assignment namespace".
Donn Cave, donn at drizzle.com
More information about the Python-list