[Python-Dev] [PEP] += on return of function call result (original) (raw)

Greg Ewing greg@cosc.canterbury.ac.nz
Thu, 03 Apr 2003 13:07:52 +1200 (NZST)


Andrew Koenig wrote:

Why can't you do this? foo = log.setdefault(r,'') foo += "test %d\n" % t

You can do it, but it's useless!

d = {} foo = d.setdefault(42, "buckle") foo += " my shoe" d {42: 'buckle'}

What Mr. Leighton wanted is impossible when the value concerned is immutable, because by the time you get to the += operator, there's no information left about where the value came from, and thus no way to update the dict with the new value.

Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+