A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2003-April/034352.html below:

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

[Python-Dev] [PEP] += on return of function call resultAndrew Koenig ark@research.att.com
02 Apr 2003 21:38:48 -0500
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



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4