0

In earlier years, i had find that:

# Python code

x = [[0]*5]*5 # IMO, it will be a 5*5 2D-Array-Like
x[2][2] = 2

# I considered x will be
# [
# [0,0,0,0,0],
# [0,0,0,0,0],
# [0,0,2,0,0],
# [0,0,0,0,0],
# [0,0,0,0,0]]

# But x will be
# [
# [0,0,2,0,0],
# [0,0,2,0,0],
# [0,0,2,0,0],
# [0,0,2,0,0],
# [0,0,2,0,0]]

So, why? Of course, later i had known this is because references of inner list. I do not thinks it is a good idea.

But again:

x[:][4] = 4

IMO again:

x[:] must be x's self. x[:][4] must be the last member of x. So x will be:

#[
# [0,0,2,0,0],
# [0,0,2,0,0],
# [0,0,2,0,0],
# [0,0,2,0,0],
# 4]

But it is not, there is no change!

So, why?

And, are there any other wired cases in Python? Please show below.

I just wanna collect things looks a bit strange in Python, not just a quizzer about this question.

nagexiucai
  • 37
  • 1
  • 7
  • If this behavior surprises you, you **must** watch [facts and myths about Python names and values](https://www.youtube.com/watch?v=_AEJHKGk9ns). – timgeb May 25 '18 at 08:17
  • Do you know the difference between reference and value? If you don't, that's hard to explain why... – Sraw May 25 '18 at 08:17

0 Answers0