Is the right-hand side of an assignment always evaluated before the assignment?

前端 未结 2 1865
遥遥无期
遥遥无期 2020-12-01 20:18

Here is a code snippet.

x = {}
x[1] = len(x)

print x
{1: 0}

Is this well defined? That is, could x == {1: 1} instead?

2条回答
  •  悲哀的现实
    2020-12-01 20:55

    Yes, it's defined. len() is called before the assignment. However, dict's are not ordered in Python, which is why you sometimes see 0, 1 and 1, 0 in the output.

提交回复
热议问题