A question on modification of a list via a function invocation

Larry Hudson orgnut at yahoo.com
Tue Aug 15 14:47:34 EDT 2017


On 08/14/2017 08:25 PM, Larry Hudson wrote:
[snip]

> Here is my attempt to clarify the situation with some ascii graphics.
> (Well, not ascii, but utf-8 box-drawing characters — I hope they come through ok.
> And, of curse, it won't display properly with a proportional font.)
> 
> The left side is the program lines, and the right side tries to show the way Python implements 
> the name binding to the data in memory.  (But I abbreviated the long assignment line,
> alist[0],alist[1],alist[2]=3,6,9 to <assignment>)
> 
> Program line          Variable bound to memory
> 
> ===========  Initial assignment  ============
> 
> ss = [1, 2, 3]              ss ───> [1, 2, 3]
> 
> ===============  test() code  ===============
> 
> def test(alist):            ss ─┬─> [1, 2, 3]
>                           alist ─┘
> ---------------------------------------------
>      alist = [3, 6, 9]       ss ───> [1, 2, 3]
>                           alist ───> [3, 6, 9]
> ---------------------------------------------
>      return                  ss ───> [1, 2, 3]
>                           alist <Garbage collected>
> 
> ===============  test1() code  ==============
> def test1(alist):           ss ─┬─> [1, 2, 3]
>                           alist ─┘
> ---------------------------------------------
>      <assignment>            ss ─┬─> [3, 6, 9]
>                           alist ─┘
> ---------------------------------------------
>      return                  ss ───> [3, 6, 9]
>                           alist <Garbage collected>
> 
> ===============  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]
> ---------------------------------------------
>      return                  ss ───> [3, 6, 9]
>                           alist <Garbage collected>
> 

This needs a minor clarification to the test1 example, plus I want to emphasize that the 
assignment here is through the alist variable.

===============  test1() code  ==============
def test1(alist):            ss ─┬─> [1, 2, 3]
                           alist ─┘
---------------------------------------------
      <assignment>            ss ─┬─> [3, 6, 9]
                           alist ─┘
---------------------------------------------
      return                  ss ───> [3, 6, 9]
                           alist <Discarded>      There is nothing to garbage collect here.


-- 
      -=- Larry -=-



More information about the Python-list mailing list