[Python-Dev] [PEP] += on return of function call result
Samuele Pedroni
pedronis@bluewin.ch
Mon, 19 May 2003 18:07:08 +0200
At 12:53 19.05.2003 +0000, Luke Kenneth Casson Leighton wrote:
>jeff,
>
>beat bolli's code example:
>
> count[word] = count.get(word, 0) + 1
>
>i think best illustrates what issue you are trying to raise.
>
>okay, we know there are two issues so let's give an example
>that removes one of those issues:
>
> count = {}
>
> count[word] = count.get(word, []) + ['hello']
>
>the issue is that the difference between the above 'hello'
>example and this:
>
> count.get(word, []) += ['hello']
>
>is that you don't know what STORE to use after the use of get()
>in the second example, but you do in the first example because
>it's explicity set out.
>
>so, does this help illustrate what might be done?
>
>if it's possible to return a result and know what should be done
>with it, then surely it should be possible to return a result from
>a += "function" and know what should be done with it?
>
>l.
>>> def refiadd(r,v): # r+=v, r is a reference, not a an lvalue
... if hasattr(r.__class__,'__iadd__'):
... r.__class__.__iadd__(r,v)
... else:
... raise ValueError,"non-sense"
...
>>> greetings={}
>>> refiadd(greetings.setdefault('susy',[]),['hello']) #
greetings.setdefault('s
usy',[]) += ['hello']
>>> refiadd(greetings.setdefault('susy',[]),['!']) #
greetings.setdefault('susy'
,[]) += ['!']
>>> greetings
{'susy': ['hello', '!']}
>>> refiadd(greetings.setdefault('betty',1),1) #
greetings.setdefault('susy',1)
+= 1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 5, in refiadd
ValueError: non-sense
regards.