Class methods in Python/C?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Ringer

    #1

    Class methods in Python/C?

    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

    #2
    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_met hod(some_arg_li st):
    pass
    some_static_met hod = staticmethod(so me_static_metho d)

    Then call the function as:
    Demo.some_stati c_method(some_a rgs)

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

    class Demo(object):
    @staticmethod
    def some_static_met hod(some_arg_li st):
    pass

    Cheers,
    Nick.

    Comment

    • Craig Ringer

      #3
      Re: Class methods in Python/C? [ANSWER]

      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:



      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

      Comment

      Working...