Writing a CPython extension - calling another sibbling method ?

Michael Torrie torriem at gmail.com
Mon Nov 18 19:18:50 EST 2019


On 11/18/19 1:15 PM, R.Wieser wrote:
> The thing is that the arguments of py_proc1 and py_proc2 are the same, but 
> for a single argument.

Does this have to be done in the C API?  Depending on how this class is
used in your Python code, I would just create a new Python class that
extends this class defined in the C code.  Then it's more a matter of:

import cmodule

class NewClass(cmodule.OldClass):

	def my_proc2(self, *args):
		b=3
		self.my_proc1( *((b,) + args) ) #OldClass.my_proc1

Now any instance of NewClass has a method called my_proc2 which calls
the my_proc1 from the C API defined class with the extra argument prepended.

The "*" notation is to unpack the tuple, which is used when calling
another function that takes positional arguments.


More information about the Python-list mailing list