Implementing class methods in C

BranoZ zarnovican at gmail.com
Fri Aug 19 06:58:15 EDT 2005


nmichaud at jhu.edu wrote:
> Am I misunderstanding the purpose of PyObject* self?

No. I think you do everything right, but it still doesn't work.

I have tried to implement it in Python:
----------------------------------------------------
_test.py:

def func2(self, *args):
  print type(self)
  print args

then all of this works:
----------------------------------------------------

import _test

class Test1:
  func2 = _test.func2

class Test2:
  pass

Test2.func2 = _test.func2

class Test3:
  pass

import new
Test3.func2 = new.instancemethod(_test.func2, None, Test3)
del new

Test1().func2(1, 2, 3)
Test2().func2(1, 2, 3)
Test3().func2(1, 2, 3)

If you implement _test in C, works none of the above.
The only difference I can see is that:

type(_test.func2)
<type 'function'>
is for Python implemented function and

type(_test.func2)
<type 'builtin_function_or_method'>
for C implementation

I would really like to know the answer too.
How do you implement some methods in C without subclassing ?

BranoZ




More information about the Python-list mailing list