This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: weird bug while using a for loop and array
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Zach kuunka, mark.dickinson
Priority: normal Keywords:

Created on 2019-11-15 07:06 by Zach kuunka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
brokentest.py Zach kuunka, 2019-11-15 07:06 The thing that I found that creates this error
Messages (4)
msg356648 - (view) Author: Zach kuunka (Zach kuunka) * Date: 2019-11-15 07:06
I haven't used this bug reporting thing before so sorry if I mess something up.

Anyway i'm not sure exactly what is causing the issue but the issue appears when you have a for loop looping through an array and you make a variable and set it to that array and append something to it. If you do the same thing with numbers it works as expected but if you do it with an array it for some reason doesn't get reset every iteration. Run this and you'll see what i'm talking about.

Arr = [1,2,3,4,5]
for num in Arr:
    Arr2 = Arr
    Arr2.append(1) #the 1 can be anything
    print(Arr2)

Also i'm interested to know why this happens, Thank You
msg356650 - (view) Author: Zach kuunka (Zach kuunka) * Date: 2019-11-15 07:36
I found out it just happens when you set an array to an array they behave as the same thing, which if its not a bug it sure is counter-intuitive. 

if you do
x = 1
then 
y = x
then add one to y you have y = 2 and x = 1
if you do it for an array
arr1 = [1]
then set something equal to that
arr2 = arr1
you essentially have 1 var that act on each other which is odd
so arr2.append(2) or arr1.append(2) make both arr1 and arr2 equal to the same thing
msg356651 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-11-15 08:25
This isn't a bug: it's the way that Python's assignment model works.

This is explained in various places (including the official Python documentation), but you may find this excellent presentation by Ned Batchelder useful: https://nedbatchelder.com/text/names1.html
msg356674 - (view) Author: Zach kuunka (Zach kuunka) * Date: 2019-11-15 13:47
I will check that out, Thank you
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82989
2019-11-15 13:47:52Zach kuunkasetmessages: + msg356674
2019-11-15 08:25:52mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg356651

resolution: not a bug
stage: resolved
2019-11-15 07:36:36Zach kuunkasetmessages: + msg356650
2019-11-15 07:06:57Zach kuunkacreate