473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

subclassing pyrex extension types in python

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

Jan 19 '07 #1
1 1172
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
Jan 28 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Kyler Laird | last post by:
I mentioned earlier that I started using Pyrex because I'm taking a computer vision course that requires all assignments to be submitted as C(++). While I could write C it would hurt me to do so...
6
by: SeeBelow | last post by:
I just read "about Pyrex" at http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/version/Doc/About.html It seems that it is not compiled into machine code, as C would be, and therefore it does...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
6
by: gregory lielens | last post by:
Hello, I am currently writing python bindings to an existing C++ library, and I just encountered a problem that I hope has been solved by more experienced python programmers: A C++ class...
1
by: Martin Bless | last post by:
Now that I've got my extension building machine using the VC++ Toolkit 2003 up and running I'm keen on using Pyrex (Pyrex-0.9.3, Python-2.4.0). But the definition of the swig_sources() method...
1
by: TPJ | last post by:
I'm trying to get a wrapper for my C code in order to be able to use it as a module in Python. I'm doing it as follows: C code (file_c.c): ------------------------------- #include <stdio.h> ...
27
by: Julien Fiore | last post by:
Do you wand to install Pyrex on Windows ? Here is a step-by-step guide explaining: A) how to install Pyrex on Windows XP. B) how to compile a Pyrex module. Julien Fiore, U. of Geneva
7
by: Jim Lewis | last post by:
I'm trying to move a function into pyrex for speed. The python side needs to pass a list to the pyrex function. Do I need to convert to array or something so pyrex can generate tight code? I'm not...
0
by: greg | last post by:
Pyrex 0.9.8.5 is now available: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ Various minor bug fixes and improvements. What is Pyrex? --------------
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.