X
X
xozzslip2015-03-21 15:50:28
Python
xozzslip, 2015-03-21 15:50:28

What's with the weird assignment behavior in python?

After a seemingly unambiguous assignment, the value changes in two places at once.

print (squares)
squares[j][orientation][1] = place
print (squares)
#j = 0, orientation = 1, place = 5

[, ]
[, ]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bobrovskyserg, 2015-03-21
@xozzslip

At indexes 1 and 3, you have the same object.
A possible solution (probably not optimal, you know better from the context) -

print (squares)
tmp=squares[j][orientation][:] #  создаём копию
tmp[1] = place
squares[j][orientation]=tmp
print (squares)

Google "mutable"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question