python里面声明多个变量 a = b = c = 1 这样有问题吗
阿神
阿神 2017-04-17 16:16:33
0
6
1040

python里面声明多个变量 a = b = c = 1 这样有问题吗
以前在 javascript 里面这样声明 var a = b = c = 1 ,b 和 c 会变成全局变量。
不知道 python 里面这样声明安全吗?

阿神
阿神

闭关修行中......

reply all(6)
小葫芦

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

刘奇

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

阿神

A problem arises when using a = b = c = []. The same address is referenced. Modifying the value of a will affect b and c

洪涛

That’s okay

if 1 < number < 10:
    print number
    
while 1 < num < 10: 
小葫芦

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;
迷茫

Just distinguish between mutable types and immutable types

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!