Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40007

Re: python 3 problem: how to convert an extension method into a class Method

From Christian Heimes <christian@python.org>
Subject Re: python 3 problem: how to convert an extension method into a class Method
Date 2013-02-26 22:08 +0100
References <512CEF0C.3020906@chamonix.reportlab.co.uk> <512D18C2.5000802@stoneleaf.us>
Newsgroups comp.lang.python
Message-ID <mailman.2577.1361912896.2939.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: python 3 problem: how to convert an extension method into a class Method Christian Heimes <christian@python.org> - 2013-02-26 22:08 +0100

csiph-web