代码之家  ›  专栏  ›  技术社区  ›  Junchao Gu

字符串为[duplicate]的奇怪有效python语法

  •  0
  • Junchao Gu  · 技术社区  · 7 年前

    这个问题已经有了答案:

    我在Python3.6.1上运行,今天遗漏了一个逗号,如下所示:

    nt = namedtuple('Record', ['c', 'a' 'b'])
    # instead the following is what I actually want
    nt = namedtuple('Record', ['c', 'a', 'b'])
    

    但我只是想知道为什么第一种方法在任何方面都是有效的python?它不应该抱怨语法错误吗?

    我刚试了3.5.2和2.7.11。似乎都是有效的。但它是有效的?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Alex Hall    7 年前

    Python tutorial :

    两个或多个字符串文字(即引号之间的文字) 彼此相邻的部分会自动连接起来。

    >>> 'Py' 'thon'
    'Python'
    
        2
  •  0
  •   grahamlyons    7 年前

    python将连接由空格分隔的相邻字符串: https://docs.python.org/2.0/ref/string-catenation.html