With or Using

Michael Hudson mwh21 at cam.ac.uk
Mon Apr 30 04:09:00 EDT 2001


"Delaney, Timothy" <tdelaney at avaya.com> writes:

> > Of course, augmented assignment was made for situations where 
> > you would like
> > to avoid writing

Nah, augmented assigment was made so that I could write:

    x += 1

inside loops and such <0.5 wink>.  Semi-seriously, I like it because
it allows me to say "add 1 to x" rather than "set x to the value of x
plus 1", and as the former is what I usually *mean*, to me it's a win.
 
> >     a.b.c().d[4].e.f.g.h().k = a.b.c().d[4].e.f.g.h().k + 1
> > 
> > allowing you instead to write
> > 
> >     a.b.c().d[4].e.f.g.h().k += 1
> 
> I am of course aware that the above is using silly made-up expressions, but
> they raise an important point.
>
> Both of the above may actually have different semantics. In each case you
> have a large number of function calls (remember, each class attribute access
> can be a function call ...). If any one of those calls returns a different
> object to the previous invocation, the two verions may be semantically
> different. In this case, binding to a temporary may be the wrong thing to do
> (or at least less of the expression should be bound to the temporary).

This is true.  OTOH, if you write 

    a.b.c().d[4].e.f.g.h().k = a.b.c().d[4].e.f.g.h().k + 1

and *don't* mean something fundamentally similar to

    a.b.c().d[4].e.f.g.h().k += 1

then you have problems all of your own.

Cheers,
M.

-- 
  I've even been known to get Marmite *near* my mouth -- but never
  actually in it yet.  Vegamite is right out.
UnicodeError: ASCII unpalatable error: vegamite found, ham expected
                                       -- Tim Peters, comp.lang.python



More information about the Python-list mailing list