Greg Ewing wrote:
> This needs consideration. Pyrex currently makes use of
> this behaviour when defining a Python class having Pyrex
> functions as methods.
>
> However, a better solution for Pyrex would be to add
> method-binding behaviour to the C function object, so
> that C functions can be used directly as methods. The
> above example would then work simply by doing
>
> Example.id = id
A C function binder is very easy to implement. Here is a rough
implementation:
class binder:
def __init__(self, func):
self._func = func
def __get__(self, obj, type):
if obj:
return wrapper(self._func, obj)
else:
return self._func
def wrapper(func, obj):
def wrapped(*args, **kwargs):
return func(obj, *args, **kwargs)
return wrapped
class Example:
id = binder(id)
>>> Example.id
<built-in function id>
>>> Example().id()
138076828
>>> Example().id
<function wrapped at 0x83ac714>
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe:
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com