Class methods in Python/C?
Thomas Heller
theller at python.net
Tue Nov 30 11:15:37 EST 2004
Jp Calderone <exarkun at divmod.com> writes:
> On Tue, 30 Nov 2004 22:39:15 +1000, Nick Coghlan <ncoghlan at email.com> wrote:
>>Craig Ringer wrote:
>> > Hi folks
>> >
>> > I've been doing some looking around, but have been unable to find out
>> > how to implement class methods on Python objects written in C. "Why are
>> > you using C?" you ask?
>> >
>> > Yeah, so do I. However, I need to provide bindings for an application
>> > that Python is embedded into, and thanks to Qt the bindings are going to
>> > be both simple and quite powerful. However, I need a way to do class
>> > methods...
>> >
>> > If anybody has any tips on this, It'd be much appreciated.
>>
>> You probably want to look at staticmethod(). (classmethod() is
>> slightly different, and probably not what you want. In fact,
>> classmethod() is practically *never* what you want. Guido wrote it
>> himself, and even he ended up not using it)
> > Funny, I find the exact opposite to be the case... ;) Static
> > methods are glorified free functions, but class methods
> > participate usefully in inheritence hierarchies - why would anyone
> > want the former?
Same for me.
To answer the original question: To create class methods in C code, you
use the METH_CLASS flag in the PyMethodDef structure. Supported in
Python 2.3 and above, in 2.2 it is more complicated. If needed, I can
post a snippet for 2.2 as well.
Thomas
More information about the Python-list
mailing list