Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Augmented Assignment question

Reply
Thread Tools

RE: Augmented Assignment question

 
 
Delaney, Timothy C (Timothy)
Guest
Posts: n/a
 
      07-17-2003
> From: Doug Tolton [(E-Mail Removed)]
>
> I mis-spoke, lists are not included. You cannot do augmented
> assignments on tuples or multiple targets.


This is still incorrect. You correct thing to say is that you cannot do an augmented assignment on something which does not have a name, or which explicitly forbids it.

> >>> a,b = 0,0
> >>> a,b += 1,1

> SyntaxError: augmented assign to tuple not possible


>>> l = []
>>> l

[]
>>> id(l)

8289204
>>> l += [1,]
>>> l

[1]
>>> id(l)

8289204
>>> l += ['foo',]
>>> l

[1, 'foo']
>>> id(l)

8289204
>>> t = ()
>>> t

()
>>> id(t)

7966916
>>> t += (1,)
>>> t

(1,)
>>> id(t)

8261268
>>> t += ('foo',)
>>> t

(1, 'foo')
>>> id(t)

8133748

Note how the ID of `l` (list) does not change, but the id of `t` (tuple) does. This is because a tuple is immutable, and so:

t += (1,)

is equivalent to:

t = t + (1,)

whereas a list is mutable, and:

l += [1,]

is equivalent to:

l.extend([1,])

Every class can decide how to implement augmented assignment, using the __iadd__, etc magic methods.

Tim Delaney

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested scopes, and augmented assignment Tim N. van der Leeuw Python 39 07-10-2006 03:05 PM
Re: [Python-Dev] The baby and the bathwater (Re: Scoping,augmented assignment, 'fast locals' - conclusion) Josiah Carlson Python 4 06-19-2006 01:34 PM
Augmented assignment Suresh Jeevanandam Python 8 02-21-2006 07:20 PM
User-defined augmented assignment Pierre Barbier de Reuille Python 4 10-01-2005 11:58 PM
Augmented Assignment question Doug Tolton Python 6 07-19-2003 12:52 AM



Advertisments