[Tutor] beginning to code
On 25-09-17 21:44, Ned Batchelder wrote:
> On 9/25/17 5:32 AM, Antoon Pardon wrote:
>> Can you explain, what you mean by "Pass-By-Reference" as far a I understand,
>> pass by reference means that the parameter of the function becomes an alias
>> of the argument, so that if the entity is mutated through one name that
>> mutation is visible through the other name.
>>> function(x, y) # allowed
>>> function(namespace.x, module.y) # allowed
>>> function(x + 1, 2) # FORBIDDEN: can't pass expressions or constants
>> Pass by reference, doesn't imply the above is forbidden. A language can
>> define, that the "x + 1" will produce an new entity and that a reference
>> to that entity will be passed.
> Wikipedia has the right definition of call by reference (https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference):
> /Call by reference/ (also referred to as /pass by reference/) is an
> evaluation strategy where a function receives an implicit reference
> <https://en.wikipedia.org/wiki/Reference_%28computer_science%29> to
> a variable used as argument, rather than a copy of its value. This
> typically means that the function can modify (i.e. assign to
> the variable used as argument?something that will be seen by its caller.
> The key idea here is that it is a reference to a *variable* that is passed, not a reference to a value. Python has no references to variables, so it cannot support call by reference. Because Python passes references to values, it is easy to call it
> "call by reference," but that is not what the term means.
And what would such a reference to a variable be exactly? If you mean the name,
languages like Pascal don't have that either. If you mean a reference to
an object/entity, Python has those.
It also says: This typically means that the function can modify the variable
used as argument, something Python can do that.
> The Wikipedia definition unfortunately includes "rather than a copy of its value," as if those are the only two options (a common misunderstanding).
> Elsewhere in this thread, someone asserted that to be call by reference, you have to be able to write a swap(x,y) function. True call-by-reference would make this possible. Python cannot do it.
That is because assignments in python don't make modifications to what
the name refers to. If you can't modify the object because the semantics
of the assignment don't do that, you can't use the impossibility of
a swap for inferring that we don't have true call by reference.
You can't write a swap in smalltalk either but the smalltalk documentation
specifies that arguments are call by reference.