A question on modification of a list via a function invocation

Ned Batchelder ned at nedbatchelder.com
Wed Aug 16 17:20:31 EDT 2017


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.

--Ned.




More information about the Python-list mailing list