[capi-sig] How to pass arguments to tp_new and tp_init from subtypes?
mateusz at loskot.net
Fri Jul 6 14:24:35 CEST 2012
I'm reading the PEP 253  on subtyping and there are plenty of good
recommendations on how to structure the types, call tp_new and tp_init
But, it lacks an important note on passing arguments from sub to super type.
It seems the PEP is unfinished as per the note:
(XXX There should be a paragraph or two about argument passing here.)
So, I'm trying to extrapolate some strategies well known from the Python
classes  subtyping, especially techniques that each level
strips-off arguments, etc.
I'm looking for techniques to achieve similar effect to this, but
in using plain Python C API (3.x):
def __init__(self, shapename, **kwds):
self.shapename = shapename
def __init__(self, color, **kwds):
self.color = color
What would be the equivalent in C API?
Also, how to deal with similar situation but with arguments
specific to derived class expected in different order, it is given
at the end of the args tuple (or kwds dict, I assume principle would be same).
Here is some (pseudo-)code that illustrates the situation:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __init__(self, x, y, a):
self.a = a
super().__init__(x, y, None):
Note, if the 'a' was expected first:
Derived.__init__(self, a, x, y)
it would be similar situation to the Shape and ColoredShape above.
It would be easier to deal with, I assume.
Mateusz Loskot, http://mateusz.loskot.net
More information about the capi-sig