Implementing class methods in C

nmichaud at jhu.edu nmichaud at jhu.edu
Fri Aug 19 10:44:22 EDT 2005


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


But the strange thing is if I use new.instancemethod the c function
becomes bound (using my previous code for _test.c)

import _test

class Test:
	def func1(self):
		print "In class "+repr(self.__class__.__namd__)

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

t = Test
type(_test.func2)	# returns <type 'builtin_function_or_method'>
type(T.func1)		# returns <unbound method Test.func>
type(t.func1)		# returns <bound method Test.func of <__main__.Test instance at 0x4eb4b8>>
type(T.func2)		# returns <built-in function func2>
type(t.func2)		# returns <bound method Test.func2 of <__main__.Test instance at 0x4eb4b8>>

So is seems like it is bound appropriately, but when called across the
C/Python api the first argument (self) of func2 is not separated
from the other arguments (and thus is not sent into the c
function as PyObject* self, but instead as part of PyObject* args)



More information about the Python-list mailing list