copy on write

Devin Jeanpierre jeanpierreda at gmail.com
Fri Jan 13 11:48:31 EST 2012


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".

And sure, it's documented. That's a bit of a cop-out though... it
isn't in the tutorial, and even if it were, it's not as if people
remember everything they read. It's not about whether you _can_ know
it as much as whether it is """obvious"". There's a bit of a feeling
that code should "do what it looks like" and be sort of understandable
without exactly understanding everything. Maybe this idea is wrong if
taken to an extreme (since it's really impossible to do completely),
but the feeling of it is probably decent. It's why we use "+" for
addition and "-" for subtraction, and not the other way around. You
don't need to know the details of operator overloading and
NotImplemented and so on to get what X + Y means for numbers, or even
for lists.

I feel like "a += b" is sort of implicitly understood by most
programmers to be the same as "a = a + b". If you asked someone what
it meant, their first answer would be "Oh, it means a = a + b"[*].
That is why it's confusing -- even to people that weren't already
exposed to that idea that these are equivalent, they get infected
fast. And then expectations get broken, because they're only *usually*
equivalent.

[*] Before posting this, I actually tried this on a Python IRC channel
-- and it happened exactly as so.

-- Devin



More information about the Python-list mailing list