【问题标题】:Why does str.split not take keyword arguments?为什么 str.split 不接受关键字参数?
【发布时间】:2012-07-27 20:05:12
【问题描述】:

我遇到了这个——在我看来——奇怪的行为:

"a b c".split(maxsplit=1)
TypeError: split() takes no keyword arguments

为什么 str.split() 不接受关键字参数,即使它是有意义的?我在 Python2 和 Python3 中都发现了这种行为。

【问题讨论】:

  • Python 3.3:工作 ideone.com/ZMACIJ
  • Python 3.6 有效。请指定哪个版本的 Python 3.x 没有?只有 2.x (2.5 .. 2.7) 似乎表现出这一点。 Issue 1176

标签: python standard-library cpython string-split


【解决方案1】:

请参阅 this bugits superseder

str.split() 是 CPython 中的原生函数,因此表现出here 描述的行为:

CPython 实现细节: 一个实现可以提供内置的 位置参数没有名称的函数,即使它们 为记录目的而“命名”,因此 不能由关键字提供。在 CPython 中,情况就是这样 用 C 实现的函数,使用 PyArg_ParseTuple() 解析它们的 论据。

【讨论】:

  • 为了自记录代码,这是一种耻辱。
  • range也有这个问题。
【解决方案2】:

str.split 是用 C 实现的内置方法。不幸的是,一些内置函数/方法不接受关键字参数。见this bug report

【讨论】:

    猜你喜欢
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2016-09-01
    • 2022-09-23
    • 2021-09-24
    • 2018-11-22
    • 2016-07-03
    相关资源
    最近更新 更多