subclassing pyrex extension types in python
greg
greg at cosc.canterbury.ac.nz
Sun Jan 28 01:58:15 EST 2007
Nitin wrote:
> I am trying to subclass an extension type in Python and add attributes
> to the new class but I keep getting errors.
>
> cdef class Spam:
>
> cdef int amount
>
> def __new__(self):
> self.amount = 0
>
> I get an error "TypeError: 'name2' is an
> invalid keyword argument for this function"
Arguments to the constructor of a class are passed
to its __new__ method as well as its __init__ method,
so if you want to subclass it in Python, you need to
allow for that by defining it as
def __new__(self, *args, **kwds):
...
Without that, your Python subclass would have to define
its own __new__ method which accepts the extra args
and strips them out, e.g.
class MySpam(Spam):
def __new__(cls, name1=None, name2=None):
return Spam.__new__(cls)
--
Greg
More information about the Python-list
mailing list