f---ing typechecking

Nick Craig-Wood nick at craig-wood.com
Wed Feb 21 12:30:10 EST 2007


Delaney, Timothy (Tim) <tdelaney at avaya.com> wrote:
>  Nick Craig-Wood wrote:
> 
> >     x += a
> > 
> > does not equal
> > 
> >     x = x + a
> > 
> > which it really should for all types of x and a
> 
>  Actually, this will *never* be the case for classes that do in-place
>  augmented assignment.
> 
>      a = [1]
>      b = [2]
> 
>      c = a + b
>      print a, b, c
> 
>      a += b
>      print a, b, c

Not sure what that is trying to show, it appears to back my point
up...

To rephrase your example

  >>> x = [1]
  >>> a = [2]
  >>> x += a
  >>> x
  [1, 2]

  >>> x = [1]
  >>> a = [2]
  >>> x = x + a
  >>> x
  [1, 2]
  >>> 

Which appears to support my point, x (and a for that matter) are the
same for both methods wheter you do x = x + a or x += a.

The mechanism is different certainly, but the result should be the
same otherwise you are breaking the basic rules of arithmetic the
programmer expects (the rule of least suprise).

>  You'll note that I didn't rebind 'a' in the non-augmented assignment. If
>  you do, augmented and non-augmented assignment may look the same, but
>  they can be very different.

Perhaps if you post a worked example from the python interpreter I'll
get what you mean!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list