Connecting Tech Pros Worldwide Help | Site Map

Class methods in Python/C?

Craig Ringer
Guest
 
Posts: n/a
#1: Jul 18 '05
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.

--
Craig Ringer

Nick Coghlan
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Class methods in Python/C?


Craig Ringer wrote:[color=blue]
> 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.[/color]

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.
Craig Ringer
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Class methods in Python/C?


On Tue, 2004-11-30 at 20:39, Nick Coghlan wrote:
[color=blue]
> 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)[/color]

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.

--
Craig Ringer

Closed Thread