Home  >  Q&A  >  body text

python里面声明多个变量 a = b = c = 1 这样有问题吗

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

阿神阿神2742 days ago1048

reply all(6)I'll reply

  • 高洛峰

    高洛峰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

    reply
    0
  • 怪我咯

    怪我咯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

    reply
    0
  • 阿神

    阿神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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:18:33

    That’s okay

    if 1 < number < 10:
        print number
        
    while 1 < num < 10: 

    reply
    0
  • 高洛峰

    高洛峰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;

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:18:33

    Just distinguish between mutable types and immutable types

    reply
    0
  • Cancelreply