A question on modification of a list via a function invocation

Mok-Kong Shen mok-kong.shen at t-online.de
Wed Aug 16 18:29:30 EDT 2017


Am 16.08.2017 um 23:20 schrieb Ned Batchelder:
> On 8/16/17 5:06 PM, Mok-Kong Shen wrote:
>> Am 15.08.2017 um 20:47 schrieb Larry Hudson:
>> [snip]
>>>> ===============  test2() code  ==============
>>>> def test2(alist):           ss ─┬─> [1, 2, 3]
>>>>                            alist ─┘
>>>> ---------------------------------------------
>>>>       <assignment>           ss ─┬─> [3, 6, 9]
>>>>                           alist ─┘
>>>> ---------------------------------------------
>>>>       alist = [30, 60, 90]    ss ───> [3, 6, 9]
>>>>                            alist ───> [30, 60, 90]
>> [snip]
>>
>> The above shows that with <assignment>, i.e. assigning single values to
>> individual members of alist (with alist[0]=3 etc.) is "principally"
>> different from assigning a whole list to alist (with alist=[30,60,90]).
>> The first operation doesn't affect the connection between ss and alist,
>> while the second separates the connection between ss and alist, as your
>> diagram above clearly indicates.
>>
>> Isn't this kind of convention/rule something that appears to be not
>> quite natural/"logical" to the common users (non-experts)?
> 
> This kind of question comes up frequently, so you are right, it needs to
> be learned. But this is true of nearly everything about programming
> languages, isn't it?  Did you take a look at
> https://nedbatchelder.com/text/names1.html ?  It's the best way I know
> to explain the principles at work here.

I looked at your web page but I don't see at which place in it the issue
that I deem to be not natural/"logical" above are (anyway concretely)
explained. I apologize, if I have overlooked.

I have earlier learned some other (older) programming languages. For
these the formal parameters are either "by reference" or "by value".
In the first case, any modification of the formal parameter inside
a function affects the corresponding actual parameter of a function
call, while in the second case a copy of the actual parameter is
passed into the function so that any modification of the formal
parameter inside the function has no effect at all outside. This is
extremely clear-cut in comparison to Python, isn't it? Anyway, while
any new user of a programming language certainly can be expected to
take good efforts to learn a lot of new stuffs, I suppose it's good
for any practical programming language to minimize the cases of
surprises for those that come from other programming languages.

M. K. Shen



More information about the Python-list mailing list