A Summary: Expression-Assignments. (Very Long)

William Tanksley wtanksle at dolphin.openprojects.net
Fri May 14 15:47:02 EDT 1999


On Fri, 14 May 1999 14:40:38 GMT, Michael P. Reilly wrote:

>Since assignment works on references to variables, this can lead to some
>complications.  Also, there are a good number of ambiguities that result
>from working with overloaded objects.

>There was a whole thread on this that is probably still on dejanews
>(don't have a reference point for that tho).  But one example of this
>ambiguity is:

>  x += B() # should x now result in an integer or an instance of B?

That doesn't seem very hard -- the variable x should contain
contents(x)+B(), whatever that may be.

>Is it safe to make some assumptions?  Granted, this is a simple
>example, and the assumption is probably clear (hey, I've only had one
>cup of coffee, my examples can't get too complex yet ;).  But there are
>plenty of other situations where the semantics wouldn't be clear from
>the statement.

I don't think so.

Nor do I really care -- I'm not agitating for +=.  I don't need it.

>More importantly, how many times does "x" get evaluated in such a
>statement?  And with some objects that can become important (database
>objects, etc.).  Is += implimented as a new syntax to evaluate to "x =
>x + y" or as a new special method like __add__, or as a combination?

x gets evaluated once to produce a storage location, then the storage
location is dereferenced.

>Usually it is better to be clear and more verbose (the age old wisdom
>of "When in doubt, parenthesize").  This is a basic tenet of Python
>(correct me if I'm getting off the mark, Guido :).

Verbosity is NOT equivalent to clarity.  (Of course, neither is +=.)

>  -Arcege

-- 
-William "Billy" Tanksley
"But you shall not escape my iambics."
           -- Gaius Valerius Catullus




More information about the Python-list mailing list