Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subclassing pyrex extension types in python

Reply
Thread Tools

subclassing pyrex extension types in python

 
 
Nitin
Guest
Posts: n/a
 
      01-19-2007
Hi All

I am trying to subclass an extension type in Python and add attributes
to the new class but I keep getting errors. I read the "Extension
Types" document on the Pyrex website but I couldn't get an answer from
it.

Here's the Spam extension type from Pyrex website:

cdef class Spam:

cdef int amount

def __new__(self):
self.amount = 0

def get_amount(self):
return self.amount

Once compiled, here's how I am using this:

import spam

class mySpam(spam.Spam):
def __init__(self, name1=None, name2=None):
spam.Spam.__init__(self)
self.name1 = name1
self.name2 = name2

When I run this Python code, I get an error "TypeError: 'name2' is an
invalid keyword argument for this function"

Is there something I need to know about Pyrex extension types and
keyword arguments ? I tried to google for this but couldn't come up
with anything.

Thanks !
Nitin

 
Reply With Quote
 
 
 
 
greg
Guest
Posts: n/a
 
      01-28-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subclassing Python types zzbbaadd@aol.com Python 11 08-30-2007 09:12 PM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
Re: [Pyrex] Allocating an array.array of a specific length in pyrex Chris Lambacher Python 0 06-08-2005 06:56 PM
Re: Pyrex without Python (was Re: calling Pyrex results from C) =?iso-8859-1?Q?Fran=E7ois?= Pinard Python 3 01-21-2004 01:53 PM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57