[Python-3000] Binding builtin function to class

Christian Heimes lists at cheimes.de
Mon Apr 28 19:14:51 CEST 2008


Haoyu Bai schrieb:
> I know this is not a bug, but however it is an exception in the
> language, what Python trying to avoid.
> 
> Since all C function in extension module is treated as builtin function
> or method, the problem maybe bigger than it looks like. In the SWIG's
> case, it originally uses new.instancemethod to generate unbound method
> from the C function in DLL module. The code snippet looks like this:
> 
> class TestBase(object):
>     """Proxy of C++ TestBase class"""
>     #some unrelated code omitted
>     pass
> #_test.TestBase_test is the C function in _test DLL module
> TestBase.test = new.instancemethod(_test.TestBase_test,None,TestBase)
> 
> Is there a corresponding way to do it in Python 3? A workaround I found is:
> 
> from types import MethodType
> class TestBase(object):
>     """Proxy of C++ TestBase class"""
>     def __init__(self, *args):
>         #some initialization code
>         ...
>         self.test = MethodType(_test.TestBase_test, self)
> 
> But this changed the original code structure so the migration would be
> more complicated. Is there any better way to get rid of it?


I've implemented a wrapper for your problem a while ago. It's in
Object/classobject.c:PyInstanceMethod_Type.

The wrapper is currently not available in Python code. But it's very
easy to make it public.

Christian



More information about the Python-3000 mailing list