30

I came across this - in my view - strange behaviour:

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

Why does str.split() not take keyword arguments, even though it would make sense? I found this behavior both in Python2 and Python3.

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
  • 1
    Python 3.3: works http://ideone.com/ZMACIJ – Smit Johnth Jun 22 '15 at 23:56
  • Python 3.6 works. Please specify which version of Python 3.x didn't? Only 2.x (2.5 .. 2.7) seem to exhibit this. [Issue 1176](https://bugs.python.org/issue1176) – smci Sep 03 '18 at 07:18

2 Answers2

32

See this bug and its superseder.

str.split() is a native function in CPython, and as such exhibits the behavior described here:

CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use PyArg_ParseTuple() to parse their arguments.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
15

str.split is a builtin method implemented in C. Unfortunately some builtin functions/methods do not accept keyword arguments. See this bug report.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384