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

Boris Boutillier boris.boutillier@arteris.net
03 Apr 2003 08:09:11 +0200


Thre is a way to do it, even with immutable objects, it is a little bit
heavier :
>>> x = {}
>>> x.setdefault(42,'buckle')
'buckle'
>>> x[42] += '3'
>>> x
{42: 'buckle3'}


Boris Boutillier,

        - ARTERIS -
Artwork Interconnecting System
6, Parc Ariane
78284 Guyancourt (FRANCE)

On Thu, 2003-04-03 at 05:04, Greg Ewing wrote:
> Andrew Koenig <ark@research.att.com>:
> 
> > 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.
> 
> I assumed that the code snippet was from his actual application, in
> which case he *did* want it to work on strings, in which case, even if
> he had the feature he wanted, it wouldn't have helped him.
> 
> I think the fact that this would only work when the value was mutable
> is a good reason to disallow it.  Too big a source of surprises,
> otherwise.
> 
> Being forced to find another way to update the value in this case is a
> feature, because the absence of such a way when the value is immutable
> makes it clear that there's no way to do what you're trying to do!
> 
> 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	   +--------------------------------------+
> 
> _______________________________________________
> Python-Dev mailing list
> Python-Dev@python.org
> http://mail.python.org/mailman/listinfo/python-dev