Using PyArg_ParseTuple to with optional fields.

Anthony Flury anthony.flury at
Thu Feb 28 04:18:22 EST 2019

I am trying to write an extension module with a function (actually an 
__init__ method, but I am not sure that matters) where the function can 
be called as either :



     my_func( a, b, c, d) - where a,b,c,d are all doubles.

I would prefer not to allow the function to be called with some 
arguments specified - it is either all four - or none.

I have tried the following format strings to PyArg_ParseTuple :

  * "dddd" - but this a mandatory 4 doubles - doesn't allow for the no
    argument scenario
  * "|dddd" - allows no arguments - but also allows only one, or just
    two, or three - i.e. not all or nothing
  * |(dddd) - allows no argument, but passing 4 arguments expects a
    4-tuple - i.e. my_func( (a,b,c,d) ) which is unnecessarily complex

Can I do what I want with a single call to PyArg_ParseTuple  ?

Anthony Flury
*Email* : anthony.flury at <mailto:Anthony.flury at>
*Twitter* : @TonyFlury <>

More information about the Python-list mailing list