简体   繁体   English

在Python AST中,什么时候解压缩的任务的目标是列表而不是元组?

[英]In Python ASTs, when would the targets of an unpacked assignment be a list instead of a tuple?

According to the GreenTreeSnakes documentation on Assignment statements : 根据有关Assignment语句GreenTreeSnakes文档

An assignment. 一项作业。 targets is a list of nodes, and value is a single node. targets是节点列表, value是单个节点。

Multiple nodes in targets represents assigning the same value to each. targets多个节点表示为每个节点分配相同的值。 Unpacking is represented by putting a Tuple or List within targets . 打开包装是通过将TupleList放入targets

My question is, when does the unpacking put the targets in a List instead of a Tuple? 我的问题是,何时解包将目标放到列表而不是元组中? The example given unpacks into a Tuple. 给出的示例将打包成一个元组。

In assignments, targets can both be lists and tuples: 在分配中,目标既可以是列表也可以是元组:

a, b, c = value  # assign to a tuple of names
[a, b, c] = value  # assign to a list of names

The difference is cosmetic to Python; 不同之处在于Python的外观。 see the Assignment statement reference documentation . 请参阅赋值语句参考文档

Demo: 演示:

>>> 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())),
  ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM