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 08-06-16 om 12:33 schreef BartC:
> On 08/06/2016 10:41, Antoon Pardon wrote:
>> Op 08-06-16 om 10:47 schreef Steven D'Aprano:
>>> On Wednesday 08 June 2016 17:53, Antoon Pardon wrote:
>>>
>>>> Python could go the simula route, which has two kinds of
>>>> assignment. One with the python semantics and one with C
>>>> semantics.
>>>>
>>>> Let as use := for the C sematics assignment and <- for the
>>>> python sematics assignment. We could then do something like
>>>> the following.
>>>>
>>>> ls := [5, 8, 13, 21]
>>>> a <- ls[2]
>>>> a := 34
>>>> print ls  # [5, 8, 34, 21]
>>> What you seem to be describing is similar to reference parameter
>>> semantics from
>>> Pascal. Assignment doesn't work that way in C, or Python.
>>
>> I disagree. In python the assignment does work similar to the
>> reference parameter
>> semantics in pascal. See the following
>>
>>   A = range[4]
>>   B = A
>>   B[2] = 5
>>   print A # [0, 1, 5, 2]
>
> (Did you mean range(4) and [0, 1, 5, 3]?)

Yes, sorry about that.

>
> But a 'proper' reference allows a complete replacement of what it
> refers to. That would mean being able to do:
>
>   B = "Cat"
>   print A     # "Cat"
>
> No tricks involving in-place updates such as assigning to list
> elements are needed.

No it doesn't mean that. It means that if you mutate the object through one variable,
you can see the result of that mutation through the other variable. But if the
assignment doesn't mutate, you can't have such effect through assignment.

In python, you can sometimes simulate a mutating assignment and then we get this.

    >>> A = [8, 5, 3, 2]
    >>> B = A
    >>> B[:] = [3, 5, 8, 13]
    >>> A
    [3, 5, 8, 13]