classification
Title: The Console of Python 3.7.0.
Type: behavior Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Shane Smith, eric.smith, iuliananet, steven.daprano
Priority: normal Keywords:

Created on 2018-11-15 13:30 by iuliananet, last changed 2018-11-15 21:19 by eric.smith. This issue is now closed.

Files
File name Uploaded Description Edit
tema_2-2-PT-PYTHON--TRACKER-text.txt iuliananet, 2018-11-15 13:40
Messages (6)
msg329949 - (view) Author: Iuliana Netoi (iuliananet) Date: 2018-11-15 13:30
While running a function that uses a list to which I append the element "z", the Console modifies my code by adding "z" to the list each time I consequently run the function. 

Is that a bug?

My function written in the Editor of Python 3.7.0. is the following:

#%%
my_list = ["this","is","my","very","first","list"]
def problem2_2(my_list):
      my_list.append("z")
      print(my_list)  
    #%%

I click the "Run" icon in the Menu. Then I type problem2_2(my_list) in the CONSOLE.

At the first execution it is ok. 
But when executing right again, the console adds another element "z" to my list. And so on, for each next execution - as if the Console modifies my code. 

(Anyway when reverting to the Editor, click inside the cell, and then "Run" icon in the Menu, only after the first execution it is printed the right number of elements)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

There is the Console executions:

mmy_list = ["this","is","my","very","first","list"]
def problem2_2(my_list):
      my_list.append("z")
      print(my_list)

problem2_2(my_list)
['this', 'is', 'my', 'very', 'first', 'list', 'z']

problem2_2(my_list)
['this', 'is', 'my', 'very', 'first', 'list', 'z', 'z']

problem2_2(my_list)
['this', 'is', 'my', 'very', 'first', 'list', 'z', 'z', 'z']

problem2_2(my_list)
['this', 'is', 'my', 'very', 'first', 'list', 'z', 'z', 'z', 'z']
msg329950 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-11-15 14:50
I don't understand what you think is the bug. You keep repeatedly appending 'z' to the same list. Why are you surprised that it appends 'z' more than once? If you don't want to append it twice, don't call the function twice.

Every time you call the function, it adds another 'z'. That's what the function does. It is working correctly.

Can you explain what you think is the bug, because it looks to me like it is working as intended.
msg329953 - (view) Author: Shane (Shane Smith) Date: 2018-11-15 16:52
I suspect the author simply does not realize lists are mutable in Python.
msg329964 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-11-15 20:05
Agreed that this is a misunderstanding and not a bug.

iuliananet: you're modifying the same list each time you're calling problem2_2(). You might look for help on the python-tutors mailing list: https://mail.python.org/mailman/listinfo/tutor
msg329967 - (view) Author: Iuliana Netoi (iuliananet) Date: 2018-11-15 21:17
Now I wrote the same list named my_list inside the function. And I deleted the function argument.When running again and again, the function remains as I defined it.
So the console behavior depends on the list placing: before or inside the function.
When the list is placed before the function and the function is called again and again, the console adds each time the element "z", enlarging the initial list.When the list is placed inside the function and the function is called again and again, the console does not add elements at all and the list remains as I defined it, so the initial one.
;;;;;;;;;;;;;;;;;;;;
#%%def problem2_2():      my_list = ["this","is","my","very","first","list"]      my_list.append("z")      print(my_list)      #%%;;;;;;;;;;;;;;;;;;;
 Here is the output, with no change in my list:
In [1]: def problem2_2():   ...:       my_list = ["this","is","my","very","first","list"]   ...:       my_list.append("z")   ...:       print(my_list)
In [2]: problem2_2()['this', 'is', 'my', 'very', 'first', 'list', 'z']
In [3]: problem2_2()['this', 'is', 'my', 'very', 'first', 'list', 'z']
In [4]: problem2_2()['this', 'is', 'my', 'very', 'first', 'list', 'z']
In [5]: problem2_2()['this', 'is', 'my', 'very', 'first', 'list', 'z']
In [6]:

    On Thursday, November 15, 2018, 4:50:53 PM GMT+2, Steven D'Aprano <report@bugs.python.org> wrote:  

Steven D'Aprano <steve+python@pearwood.info> added the comment:

I don't understand what you think is the bug. You keep repeatedly appending 'z' to the same list. Why are you surprised that it appends 'z' more than once? If you don't want to append it twice, don't call the function twice.

Every time you call the function, it adds another 'z'. That's what the function does. It is working correctly.

Can you explain what you think is the bug, because it looks to me like it is working as intended.

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue35256>
_______________________________________
msg329968 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-11-15 21:19
Yes, this is as expected.

As I said, since this isn't a bug in Python, this is not the appropriate place to discuss your problem. I suggest python-tutors list, but there are many other places to learn about python global and local variables.
History
Date User Action Args
2018-11-15 21:19:49eric.smithsetmessages: + msg329968
components: - Tests
2018-11-15 21:17:02iuliananetsetmessages: + msg329967
2018-11-15 20:05:52eric.smithsetstatus: open -> closed

nosy: + eric.smith
messages: + msg329964

resolution: not a bug
stage: resolved
2018-11-15 16:52:46Shane Smithsetnosy: + Shane Smith
messages: + msg329953
2018-11-15 14:50:49steven.dapranosetnosy: + steven.daprano
messages: + msg329950
2018-11-15 13:40:24iuliananetsetfiles: + tema_2-2-PT-PYTHON--TRACKER-text.txt
type: behavior
2018-11-15 13:30:01iuliananetcreate