Class methods in Python/C?

C

Craig Ringer

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.
 
N

Nick Coghlan

Craig said:
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)

class Demo(object):
def some_static_method(some_arg_list):
pass
some_static_method = staticmethod(some_static_method)

Then call the function as:
Demo.some_static_method(some_args)

In Py2.4, the method definition can be collapsed to:

class Demo(object):
@staticmethod
def some_static_method(some_arg_list):
pass

Cheers,
Nick.
 
C

Craig Ringer

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)

Hmm, I've always rather favoured class methods myself. However, that's
not the point - the same question can apply just as well to static
methods. I know how to construct both in Python, though I rarely use
static methods, only class methods.

What I was after is a way to define a static method in a C extension
module.

I just found it - in the library reference, of course. I'd simply missed
it before. For anybody else looking through the archives to answer this
question later:

http://docs.python.org/api/common-structs.html

It turns out there are calling convention flags to specify class methods
and static methods. From the docs:

METH_CLASS
The method will be passed the type object as the first parameter
rather than an instance of the type. This is used to create
class methods, similar to what is created when using the
classmethod() built-in function. New in version 2.3.

METH_STATIC
The method will be passed NULL as the first parameter rather
than an instance of the type. This is used to create static
methods, similar to what is created when using the
staticmethod() built-in function. New in version 2.3.

Sorry for the noise everybody, I could've sworn I looked over that
already.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top