[Python-Dev] [PEP] += on return of function call result

Luke Kenneth Casson Leighton lkcl@samba-tng.org
Wed, 2 Apr 2003 09:07:26 +0000

example code:

log = {}

	for t in range(5):
		for r in range(10):
			log.setdefault(r, '') += "test %d\n" % t


instead, as the above is not possible, the following must be used:

from operator import add


			add(log.setdefault(r, ''), "test %d\n" % t)

... ARGH!  just checked - NOPE!  add doesn't work.
and there's no function "radd" or "__radd__" in the
operator module.

unless there are really good reasons, can i recommend allowing +=
on return result of function calls.

i cannot honestly think of or believe that there is a reasonable
justification for restricting the += operator.

append() on the return result of setdefault works absolutely
fine, which is GREAT because you have no idea how long i have
been fed up of not being able to do this in one line:

	log = {}
	log.setdefault(99, []).append("test %d\n" % t)