Class methods in Python/C? [ANSWER]

Craig Ringer craig at postnewspapers.com.au
Wed Dec 1 01:20:45 EST 2004


On Tue, 2004-11-30 at 20:39, Nick Coghlan wrote:

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

--
Craig Ringer




More information about the Python-list mailing list