python 3 problem: how to convert an extension method into a class Method
Christian Heimes
christian at python.org
Tue Feb 26 16:08:02 EST 2013
Am 26.02.2013 21:19, schrieb Ethan Furman:
> Dumb question, but have you tried just assigning it? In Py3 methods are
> just normal functions...
>
> 8<----------------------
> class A():
> pass
>
> A.method = c_method
> 8<----------------------
That doesn't work with builtin functions because they don't implement
the descriptor protocol:
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Feb 8 2013, 00:38:29)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class Example:
... id = id
...
>>> Example().id()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: id() takes exactly one argument (0 given)
You can use PyInstanceMethod_Type to wrap a builtin method in something
that acts like a normal method. I implemented the type for Python 3000
when I removed the unbound method object. It's not available to pure
Python code except for testing:
>>> import _testcapi
>>> class Example:
... id = _testcapi.instancemethod(id)
...
>>> Example().id()
140525206026320
The C code is rather simple and small. You can easily re-implement in
Python, too.
More information about the Python-list
mailing list