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


Groups > comp.lang.python > #40007 > unrolled thread

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

Started byChristian Heimes <christian@python.org>
First post2013-02-26 22:08 +0100
Last post2013-02-26 22:08 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromChristian Heimes <christian@python.org>
Date2013-02-26 22:08 +0100
SubjectRe: python 3 problem: how to convert an extension method into a class Method
Message-ID<mailman.2577.1361912896.2939.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web