python里面声明多个变量 a = b = c = 1 这样有问题吗
以前在 javascript 里面这样声明 var a = b = c = 1 ,b 和 c 会变成全局变量。
不知道 python 里面这样声明安全吗?
高洛峰2017-04-17 16:18:33
Safe, but for reference types such as lists, dictionaries, and classes, a, b, and c will all point to the same reference, instead of creating three independent variables
怪我咯2017-04-17 16:18:33
Safe
When python loads other files, it needs to be explicitly imported before it can import the variables of other files (as long as you are not from XXX import *
), so there is no need to worry about the variables of the two files contaminating each other
阿神2017-04-17 16:18:33
A problem arises when using a = b = c = []. The same address is referenced. Modifying the value of a will affect b and c
高洛峰2017-04-17 16:18:33
No problem, this is a syntax unique to Python, equivalent to
a=1
b=1
c=1
And in js it will become
c=1;
b=c;
var a = b;