Class methods in Python/C?

Nick Coghlan ncoghlan at email.com
Tue Nov 30 07:39:15 EST 2004


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)

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.



More information about the Python-list mailing list