git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

I'm wrong or Will we fix the ducks limp?


Op 09-06-16 om 14:17 schreef BartC:
> On 09/06/2016 12:19, Antoon Pardon wrote:
>> Op 09-06-16 om 12:53 schreef BartC:
>>> On 09/06/2016 10:46, Antoon Pardon wrote:
>>>> Op 09-06-16 om 09:36 schreef Steven D'Aprano:
>>>
>>>>> Your example demonstrates object mutation, not assignment.
>>>>
>>>> Generally assignment and mutation don't contradict each other.
>>>> So IMO the cause is the same, a mutation. In some languages you
>>>> can mutate your variable through an assignment and in others you
>>>> can't.
>>>
>>> I think this is what is confusing you.
>>>
>>> Mutation is like repairing or customising my car.
>>>
>>> Full assignment is like buying a new car.
>>
>> I think you are confused. Full assignment of C struct or
>> Pascal record is the same as mutating all individual
>> attributes. You don't get a new record/instance as in
>> Python. You replace the value of the attributes with
>> something else.
>>
>> As far a language semantics are concerned, you get the
>> (new) car at scope entry, all assigments later are
>> mutations.
>>
>> You may think of assignment as like buying a new car,
>> that is not how the semantics of languages like pascal
>> and C are defined.
>
> Static languages like C and Pascal don't make for a good comparison. C
> is just too low level: anything is possible so can be twisted any way.
> A bit like assembly.

But this "low" level is needed to make a swap work.

>
> C variables have a fixed type, which you can't change by assignment
> (so a Ford can't change to a different marque). But being low-level,
> assignment in C is just copying N bytes from A to B. Which is also
> what happens when, instead of copying a whole struct, you mutate all
> of it by assigning the fields one by one.

Exactly and it is this mutating that makes a swap
procedure possible in the languages that come up
here.

>
> Python assignments are bit more sophisticated, and there are real
> differences between full assignment (where a variable ends up with a
> different object reference) and mutation (where it might be the same,
> but modified, object).

Yes and it is this difference that makes it impossible
to write a swap procedure, despite the variables in python
being reference variables.

Make up what ever kind of reference you wil. If your assignment
will only change references and won't mutate, you will not be
able to write a swap procedure.