A question on modification of a list via a function invocation

Ned Batchelder ned at nedbatchelder.com
Mon Aug 14 19:12:07 EDT 2017


On 8/14/17 4:18 PM, Mok-Kong Shen wrote:
> Am 14.08.2017 um 22:10 schrieb oliver:
>> It is not a global because accessing an item of a list does not change
>> whether it is global or local. It would only be global if you
>> declared it
>> global via a "global" statement. Can you give an example, that would
>> help
>> determine the issue.
>
> If without a global statement, a name is local, then alist[0]=3 should
> not work globally, if it works at all, in my layman's logic.

Take a few moments to read or watch this:
https://nedbatchelder.com/text/names1.html

It answers precisely these questions.

--Ned.

>
> M. K. Shen
>>
>> On Mon, 14 Aug 2017 at 16:06 Mok-Kong Shen <mok-kong.shen at t-online.de>
>> wrote:
>>
>>> Am 14.08.2017 um 21:53 schrieb Ned Batchelder:
>>>> On 8/14/17 3:21 PM, Mok-Kong Shen wrote:
>>>>> Am 14.08.2017 um 20:50 schrieb Ned Batchelder:
>>>>>> On 8/14/17 2:21 PM, Mok-Kong Shen wrote:
>>>>>>> I ran the attached program and got the following output:
>>>>>>>
>>>>>>> [1, 2, 3]
>>>>>>> [3, 6, 9]
>>>>>>>
>>>>>>> I don't understand why the modification doesn't work in the case of
>>>>>>> test() but does work in the case of test1().
>>>>>>>
>>>>>>> Thanks for your help in advance.
>>>>>>>
>>>>>>> M. K. Shen
>>>>>>>
>>>>>>> ------------------------------------------------------------
>>>>>>>
>>>>>>> def test(alist):
>>>>>>>      alist=[3,6,9]
>>>>>>>      return
>>>>>>>
>>>>>>> def test1(alist):
>>>>>>>      alist[0],alist[1],alist[2]=3,6,9
>>>>>>>      return
>>>>>>>
>>>>>>> ss=[1,2,3]
>>>>>>> test(ss)
>>>>>>> print(ss)
>>>>>>> test1(ss)
>>>>>>> print(ss)
>>>>>>
>>>>>> This reassigns the name alist:  alist = [3, 6, 9].   That changes
>>>>>> the
>>>>>> local variable, but cannot affect the caller's variables.
>>>>>>
>>>>>> This leaves alist as the same object, but reassigns its elements,
>>>>>> mutating the list:  alist[0] = 3
>>>>>>
>>>>>> This talk has more details:
>>>>>> https://nedbatchelder.com/text/names1.html
>>>>>
>>>>> I could more or less understand that in test() alist is
>>>>> interpreted as
>>>>> local but in the extended program below in test2() I first write the
>>>>> same as in test1(), after which I logically assume that the name
>>>>> alist
>>>>> is now known as global and then I write alist=[30,60,90] but that
>>>>> doesn't have any effect globally, since I get the output:
>>>>>
>>>>> [1, 2, 3]
>>>>> [3, 6, 9]
>>>>> [3, 6, 9]
>>>>>
>>>>> Could you please explain that?
>>>>>
>>>>> M. K. Shen
>>>>> ---------------------------------------------------------
>>>>>
>>>>> def test(alist):
>>>>>     alist=[3,6,9]
>>>>>     return
>>>>>
>>>>> def test1(alist):
>>>>>     alist[0],alist[1],alist[2]=3,6,9
>>>>>     return
>>>>>
>>>>> def test2(alist):
>>>>>     alist[0],alist[1],alist[2]=3,6,9
>>>>>     alist=[30,60,90]
>>>>>     return
>>>>>
>>>>> ss=[1,2,3]
>>>>> test(ss)
>>>>> print(ss)
>>>>> test1(ss)
>>>>> print(ss)
>>>>> test2(ss)
>>>>> print(ss)
>>>>
>>>> Your test2 function first mutates the caller's list by assigning
>>>> alist[0]=3, then it rebinds the local name alist to be a new list.  So
>>>> the caller's list is now [3, 6, 9].
>>>
>>> Sorry for my poor knowledge. After the line alist[0]..., what is the
>>> status of the name alist? It's now a global name, right? So why in the
>>> line following that the name alist would suddenly be interpreted as
>>> local? I can't yet fully comprehend the logic behind that.
>>>
>>> M. K. Shen
>>>
>>>
>>>    .
>>>
>>> -- 
>>> https://mail.python.org/mailman/listinfo/python-list
>>>
>




More information about the Python-list mailing list