
Greg> Andrew Koenig wrote:
Why can't you do this? foo = log.setdefault(r,'') foo += "test %d\n" % t
Greg> You can do it, but it's useless!
d = {} foo = d.setdefault(42, "buckle") foo += " my shoe" d Greg> {42: 'buckle'}
Greg> What Mr. Leighton wanted is *impossible* when the value Greg> concerned is immutable, because by the time you get to Greg> the += operator, there's no information left about where Greg> the value came from, and thus no way to update the Greg> dict with the new value. Of course it's impossible when the value is immutable, because += cam't mutate it :-) However, consider this: foo = [] foo += ["my shoe"] No problem, right? So the behavior of foo = d.setdefault(r,'') foo += "test %d\n" % t depends on what type foo has, and the OP didn't say. But whatever type foo might have, the behavior of the two statements above ought logically to be the same as the theoretical behavior of d.setdefault(r,'') += "test %d\n" % t which is what the OP was trying to achieve. -- Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark