最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why `ast.Assign.targets` is a list? - Stack Overflow

programmeradmin1浏览0评论

ast.Assign.targets is a list

a, b = c, d

Yields following AST:

Assign(
  targets=[
    Tuple(
      elts=[
        Name(id='a', ctx=Store()),
        Name(id='b', ctx=Store())],
      ctx=Store())],
  value=Tuple(
    elts=[
      Name(id='c', ctx=Load()),
      Name(id='d', ctx=Load())],
    ctx=Load()))

Under what conditions target would be a list with multiple elements instead of single Tuple ?

ast.Assign.targets is a list

a, b = c, d

Yields following AST:

Assign(
  targets=[
    Tuple(
      elts=[
        Name(id='a', ctx=Store()),
        Name(id='b', ctx=Store())],
      ctx=Store())],
  value=Tuple(
    elts=[
      Name(id='c', ctx=Load()),
      Name(id='d', ctx=Load())],
    ctx=Load()))

Under what conditions target would be a list with multiple elements instead of single Tuple ?

Share asked Feb 10 at 18:57 mr0re1mr0re1 1,6162 gold badges11 silver badges19 bronze badges 1
  • I do not know enough about syntax notation to answer this, but for those who do, I suspect the answer is somewhere in the # ASSIGNMENT TARGETS section of the grammar spec – juanpa.arrivillaga Commented Feb 10 at 19:14
Add a comment  | 

1 Answer 1

Reset to default 3

When there are multiple destinations for the assignment of the value. That is:

a = b = c = d

AST:

Module(
  body=[
    Assign(
      targets=[
        Name(id='a', ctx=Store()),
        Name(id='b', ctx=Store()),
        Name(id='c', ctx=Store())],
      value=Name(id='d', ctx=Load()))],
  type_ignores=[])
发布评论

评论列表(0)

  1. 暂无评论