Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Yep! It's slightly worse than you would think. Here's a (slightly edited) version of how the argument passing works

  static PyObject *
  unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  {
      PyObject *x = NULL;
      static char *kwlist[] = {"object", "encoding", "errors", 0};
      char *encoding = NULL;
      char *errors = NULL;
  
      PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:str",
                                  kwlist, &x, &encoding, &errors))
  }
Notice the call to PyArg_ParseTupleAndKeywords -- it takes an arguments tuple and a format string and executes a mini interpreter to parse the arguments from the tuple. It has to be ready to receive arguments as any combination of keywords and positional, but for a given callsite the matching will generally be static.


And is that literally every Python function doing that under the hood? Even a 1-arity function?

I don't get what the format string is for.

Anyway, thanks for answering!


It used to be this way, but at some point they added a faster calling convention and moved a number of things to it. Calling type objects still falls back to the old convention though.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: