Op 09-06-16 om 09:36 schreef Steven D'Aprano: > On Wednesday 08 June 2016 19:41, Antoon Pardon wrote: > >>> 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] >> >> This is exactly the result you would get with B as a reference parameter in >> pascal. > It might be the same result, but it is a different cause. > > 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. > But since you think that assignment in Python behaves like Pascal var > parameters (reference variables), you should be able to write a "swap" > procedure that swaps two arguments. That's the definitive test for reference > parameters, since it works in all languages with reference parameters (e.g. > Pascal, Basic and C++), and doesn't work in any language without them (or > equivalent, like Algol's pass-by-name parameters). It works in those languages because their the assignment mutates the variable that is assigned to. It is the fact that one can mutate a variable through an alias that makes it a reference variable, that is how it was already used in smalltalk. So because in python assignments don't mutate, you can't write a "swap" procedure in python, but AFAIR neither could you in smalltalk. You are confusing what it is that makes something a reference, with how an assignment can have an effect on a reference. It makes no sense to expect you can do the same with reference assignments as you can do with mutating assignments. > That is my challenge to you: if you can write such a function, in pure > Python, > which swaps two parameters given as ordinary arguments (passing their names > as > strings does not count), then I will cheerfully apologise for doubting you, > admit that I was wrong, and publicly acknowledge that Python does have > reference variables just like you said. > > If you cannot meet that challenge, then I would like you to acknowledge that > whatever Python variables are, and however Python's assignment works, it is > *not* the same as Pascal's reference variables, and so calling Python > variables > "reference variables" is misleading and an abuse of a well-established term > from computer science since long before Python even existed. Your challenge, shows that you don't fully understand what reference variables are. The behaviour you see in Pascal, doesn't depend (alone) on the parameter being a reference parameter. It also depends on the fact that the assignment in pascal mutates the variable that is assigned to. Variables are references if they are aliases, so that if you mutate through one alias, the mutation is visible through other aliases. So your challenge comes down to expecting me to mutate something by means that in python don't allow mutation. > Are you willing to accept that challenge? > > Or are you going to try to weasel out of it by changing the definition of > reference parameter to mean "whatever I want it to mean to avoid admitting I > was wrong"? I am using reference as it was already use by smalltalk. > >> It is true that you can't get such an effect in python, but that doesn't >> imply that python doesn't have reference variables (as an abstract notion). >> Because having reference variables doesn't imply you can have that effect. > Ah, well that answers that question. Weasel out it is. That is presuming you have the right understanding. -- https://mail.python.org/mailman/listinfo/python-list