no unbound methods in py3k

Thomas Heller theller at python.net
Thu Oct 9 12:24:38 CEST 2008


Christian Heimes schrieb:
> Thomas Heller wrote:
>> but this is very ugly, imo.  Is there another way?
>> The raw_func instances that I have are not descriptors (they
>> do not implement a __get__() method...)
> 
> I've written PyInstanceMethod_Type for this use case. It's not (yet) 
> available for Python code. Barry hasn't decided whether he should expose 
> the type so late in the release cycle or not. See 
> http://bugs.python.org/issue3787 and 
> http://docs.python.org/dev/3.0/c-api/method.html?highlight=pyinstancemethod#PyInstanceMethod_Type
> 

Ok, so one has to write an extension to access or expose it.

Oh, wait - there's ctypes:

Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> pythonapi.PyInstanceMethod_New.restype = py_object
>>> pythonapi.PyInstanceMethod_New.argtypes = [py_object]
>>> instancemethod = pythonapi.PyInstanceMethod_New
>>>
>>> class Example:
...     pass
...
>>> Example.id = instancemethod(id)
>>>
>>> x = Example()
>>> x.id()
12597296
>>> id(x)
12597296
>>>

Thomas



More information about the Python-list mailing list