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: List define and Change result
Type: behavior Stage:
Components: Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, martin.panter, saeed, xiang.zhang
Priority: normal Keywords:

Created on 2016-11-03 07:23 by saeed, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
my.tar.7z saeed, 2016-11-03 07:23
Messages (5)
msg279967 - (view) Author: saeed (saeed) Date: 2016-11-03 07:23
Hi,
I define multi List in a line such as this:
    smoke= exercise  = cholesterol = angina = stroke = attack = [0] * 2
and This work bad!
if I define later line such this, problem has been solve:
    smoke=[0]*2
    exercise  = [0]*2
    cholesterol = [0]*2
    angina = [0]*2
    stroke = [0]*2

I just change this line in my project but result changed!

Is this a bug?

*****************************************
*****************************************

notice to "Smoke" change in follow,
"Smoke" changed from [0, 1] to [0 ,6] in a one state, for what??!!

*****************************************
out put before change:

> $ python3.5 ./my.py
smoke=  [0, 0]
                      before income: [0, 0, 0, 0, 0, 0, 0, 0]
income[ 5 ] += 1       after income: [0, 0, 0, 0, 0, 1, 0, 0]
                      before smoke: [0, 0]
2
smoke[ 1 ] += 1       after smoke: [0, 1]
stop in step
                      before income: [0, 0, 0, 0, 0, 1, 0, 0]
income[ 1 ] += 1       after income: [0, 1, 0, 0, 0, 1, 0, 0]
                      before smoke: [0, 6]
2
smoke[ 1 ] += 1       after smoke: [0, 7]
stop in step
                      before income: [0, 1, 0, 0, 0, 1, 0, 0]
income[ 5 ] += 1       after income: [0, 1, 0, 0, 0, 2, 0, 0]
                      before smoke: [2, 10]
1
smoke[ 0 ] += 1       after smoke: [3, 10]
stop in step       

*****************************************

out put after change:
>$ python3.5 my1.py 
smoke=  [0, 0]
                      before income: [0, 0, 0, 0, 0, 0, 0, 0]
income[ 5 ] += 1       after income: [0, 0, 0, 0, 0, 1, 0, 0]
                      before smoke: [0, 0]
2
smoke[ 1 ] += 1       after smoke: [0, 1]
stop in step
                      before income: [0, 0, 0, 0, 0, 1, 0, 0]
income[ 1 ] += 1       after income: [0, 1, 0, 0, 0, 1, 0, 0]
                      before smoke: [0, 1]
2
smoke[ 1 ] += 1       after smoke: [0, 2]
stop in step
                      before income: [0, 1, 0, 0, 0, 1, 0, 0]
income[ 5 ] += 1       after income: [0, 1, 0, 0, 0, 2, 0, 0]
                      before smoke: [0, 2]
1
smoke[ 0 ] += 1       after smoke: [1, 2]
stop in step

*****************************************

:/

Thanks
msg279969 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-11-03 07:51
I haven’t looked at your code, but I think you may have misunderstood how variable assignments work in Python. See <https://docs.python.org/3.5/faq/programming.html#why-did-changing-list-y-also-change-list-x>.

When you assign an object such as [0, 0] to a variable name, the name is “bound” to the object. When you assign to a second name, the second name is bound to exactly the same object. When you alter the object through one name, the change is visible from any of the bound names.

Closing this, but let me know if I got it wrong.
msg279972 - (view) Author: saeed (saeed) Date: 2016-11-03 08:10
Thank for attention,but see this:

>>> x=y=[]
>>> x=y=[0]*3
>>> x
[0, 0, 0]
>>> y
[0, 0, 0]
>>> y=[0, 1, 1]
>>> x
[0, 0, 0]
>>> y
[0, 1, 1]
>>> y[1]+=1
>>> y
[0, 2, 1]
>>> x
[0, 0, 0]
>>> x[0]+=5
>>> x
[5, 0, 0]
>>> y
[0, 2, 1]

then must my code work and there is another problem :/

*********************************

notice: I don't write like this: 
>>> x=[]
>>> y=x
msg279976 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-11-03 08:36
In your last example, after x=y=[], you reassign a new list to y, so x and y are different. In your my.py, you are altering the same list. Please read the link Martin gives.
msg279979 - (view) Author: saeed (saeed) Date: 2016-11-03 09:32
OK,
I found My problem, 
Thank so much,

There is any way to define them shortly??
History
Date User Action Args
2022-04-11 14:58:39adminsetgithub: 72780
2016-11-03 09:32:16saeedsetmessages: + msg279979
2016-11-03 08:36:49xiang.zhangsetstatus: open -> closed
nosy: + xiang.zhang
messages: + msg279976

2016-11-03 08:12:41saeedsetstatus: closed -> open
2016-11-03 08:10:38saeedsetmessages: + msg279972
2016-11-03 07:51:28martin.pantersetstatus: open -> closed

nosy: + martin.panter
messages: + msg279969

resolution: not a bug
2016-11-03 07:23:18saeedcreate