在 Python AST 中,什么时候解压赋值的目标是列表而不是元组?
In Python ASTs, when would the targets of an unpacked assignment be a list instead of a tuple?
根据 GreenTreeSnakes documentation on Assignment statements:
An assignment. targets
is a list of nodes, and value
is a single node.
Multiple nodes in targets
represents assigning the same value to each.
Unpacking is represented by putting a Tuple
or List
within targets
.
我的问题是,什么时候解包将目标放在列表而不是元组中?给出的示例解包成元组。
在赋值中,目标既可以是列表也可以是元组:
a, b, c = value # assign to a tuple of names
[a, b, c] = value # assign to a list of names
差异只是装饰性的 Python;见 Assignment statement reference documentation.
演示:
>>> parseprint('[a, b, c] = value')
Module(body=[
Assign(targets=[
List(elts=[
Name(id='a', ctx=Store()),
Name(id='b', ctx=Store()),
Name(id='c', ctx=Store()),
], ctx=Store()),
], value=Name(id='value', ctx=Load())),
])
根据 GreenTreeSnakes documentation on Assignment statements:
An assignment.
targets
is a list of nodes, andvalue
is a single node.Multiple nodes in
targets
represents assigning the same value to each. Unpacking is represented by putting aTuple
orList
withintargets
.
我的问题是,什么时候解包将目标放在列表而不是元组中?给出的示例解包成元组。
在赋值中,目标既可以是列表也可以是元组:
a, b, c = value # assign to a tuple of names
[a, b, c] = value # assign to a list of names
差异只是装饰性的 Python;见 Assignment statement reference documentation.
演示:
>>> parseprint('[a, b, c] = value')
Module(body=[
Assign(targets=[
List(elts=[
Name(id='a', ctx=Store()),
Name(id='b', ctx=Store()),
Name(id='c', ctx=Store()),
], ctx=Store()),
], value=Name(id='value', ctx=Load())),
])