PyArg_ParseTuple() Cheat Sheet - danecross/PyNA62Analysis GitHub Wiki

The function signature for this method is as follows:

int PyArg_ParseTuple (PyObject *arg, const char *format, ...)

Argument descriptions:

  • *arg is the python tuple of arguments.
  • *format argument is the format string. More on this in 3 lines
  • the remaining arguments are addresses for references in the format string

Format String

This is a string of characters which consists of zero or more "format units." Each unit describes one of the python objects in the *arg tuple by saying what Python type it is, and what C type it must be converted to.

There is a long list of these format units. I will list the ones I think will be relevant to us, for a quick reference. As in the documentation, (round) brackets describe the python type and [square] brackets describe the C type it will be translated to.

Strings and Buffers

  • s (str)[const char *]
  • s* (str or bytes-like obj)[Py_buffer]
  • z (str or None)[const char *]

Numbers

  • i (int)[int]
  • I (int)[unsigned int]
  • l (int)[long int]
  • c (bytes or bytearray of len 1)[char]
  • C (str of len 1)[int]
  • f (float)[float]
  • d (float)[double]
  • D (complex)[Py_complex]

Objects and Misc.

  • O (object)[PyObject *]
  • O! (object)[typeobject, PyObject]
  • p (bool)[int]
  • (items) (tuple)[matching items]