I ran into example code that uses a statement with the same variable:
我遇到了使用相同变量的语句的示例代码:
event_type=event_type=str(payload_json['event'])
I have tried:
我有尝试:
a=b=c=d=10
and all four variables (a, b, c and d) all become 10. Like:
所有四个变量(a, b, c和d)都变成了10。如:
a=10
b=10
c=10
d=10
This is an Amazon code example so I doubt my understanding of Python rather than the code example. The page can be found here: AWS Kinesis example
这是一个Amazon代码示例,因此我怀疑我对Python的理解,而不是代码示例。这一页可以在这里找到:AWS Kinesis范例
What is likely happening here? Some Python voodoo I don't understand or just a typo?
这里可能会发生什么?一些我不懂的Python巫术还是一个拼写错误?
1 个解决方案
#1
2
a = a = b
is always equivalent to a = b
in python. Using statements with multiple equal signs as you describe is called chained assignment and is supported in many programming languages. Some languages will raise an error upon detecting chained assignment of the same variable (C), but others simply ignore it (python, javascript).
在python中,a = a = b总是等价于a = b。如您所述,使用带有多个等号的语句称为链式赋值,在许多编程语言中都支持这种方法。有些语言在检测相同变量(C)的链式赋值时会产生错误,而其他语言则会忽略它(python、javascript)。
It would be a bad idea to change this behavior, and not easy to achieve because the assignment operator's behavior is built in to python with no modifcation hooks provided (see: Is it possible to overload Python assignment?). Thus I think it is safe to assume that this is a (harmless) typo you have uncovered.
改变这种行为不是一个好主意,而且不容易实现,因为赋值操作符的行为构建在python中,不提供任何修改钩子(参见:是否可能重载python赋值?)因此,我认为可以肯定的是,这是您已经发现的(无害的)错误。
#1
2
a = a = b
is always equivalent to a = b
in python. Using statements with multiple equal signs as you describe is called chained assignment and is supported in many programming languages. Some languages will raise an error upon detecting chained assignment of the same variable (C), but others simply ignore it (python, javascript).
在python中,a = a = b总是等价于a = b。如您所述,使用带有多个等号的语句称为链式赋值,在许多编程语言中都支持这种方法。有些语言在检测相同变量(C)的链式赋值时会产生错误,而其他语言则会忽略它(python、javascript)。
It would be a bad idea to change this behavior, and not easy to achieve because the assignment operator's behavior is built in to python with no modifcation hooks provided (see: Is it possible to overload Python assignment?). Thus I think it is safe to assume that this is a (harmless) typo you have uncovered.
改变这种行为不是一个好主意,而且不容易实现,因为赋值操作符的行为构建在python中,不提供任何修改钩子(参见:是否可能重载python赋值?)因此,我认为可以肯定的是,这是您已经发现的(无害的)错误。