copy on write
neilc at norwich.edu
Fri Jan 13 11:54:35 EST 2012
On 2012-01-13, Devin Jeanpierre <jeanpierreda at gmail.com> wrote:
> On Fri, Jan 13, 2012 at 10:13 AM, Grant Edwards <invalid at invalid.invalid> wrote:
>> On 2012-01-13, Devin Jeanpierre <jeanpierreda at gmail.com> wrote:
>>> On Fri, Jan 13, 2012 at 7:30 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>>> It seems there's a distinct difference between a+=b (in-place
>>>> addition/concatenation) and a=a+b (always rebinding), which is sorely
>>>> confusing to C programmers. But then, there's a lot about Python
>>>> that's sorely confusing to C programmers.
>>> I think this is confusing to just about everyone, when they first
>>> encounter it.
>> That depends on what languages they've used in the past and whether
>> they skip reading any documentation and just assume that all languages
>> work the same way.
>> I would agree that for the majority of new users, they previously used
>> only languages where an assignment operator does a "copy value", and
>> that 90+ percent of the time those new users they assume all languages
>> work that way.
> That isn't what I was referring to. Specifically, it confuses
> almost everyone the first time they encounter it that "a += b"
> is not the same as "a = a + b".
If you've ever implemented operator=, operator+, and operator+=
in C++ you'll know how and why they are different. A C++
programmer would be wondering how either can work on immutable
objects, and that's where Python's magical rebinding semantics
come into play.
More information about the Python-list