Turning builtin functions into methods.

  • Thread starter Jacek Generowicz
  • Start date
J

Jacek Generowicz

Functions defined in Python have type types.FunctionType, and are
descriptors whose __get__ method turns them into bound or unbound
methods. Functions defined in extension modules have type
types.BuiltinFunctionType, and have no __get__ method. Adding them as
attributes to classes and calling them through an instance of the
class does not result in them being called as methods: self is lost.

What's the simplest way of getting around this ?

Some background: I'm trying to speed up my application by recoding, in
C, the Python functions which are being called in my inner loops. I
have a number of classes which have a single method being called in
the inner loops, so, rather than recoding the whole class, I'd like to
recode only the relevant method, and glue it onto the class, like this

class foo:
def this(self, ...):
...
def that(self, ...):
...

import speedup

foo.the_other = speedup.the_other

where the_other is implemented in C, but is equivalent to

def the_other(self, ...):
...


Any suggestions ?

Thanks,



PS. I'm a bit disappointed that Python makes this distinction between
functions defined in extension modules, and ones defined in pure
Python, but I guess that there are good practical reasons for it.
 
J

Jacek Generowicz

Jacek Generowicz said:
Functions defined in Python have type types.FunctionType, and are
descriptors whose __get__ method turns them into bound or unbound
methods. Functions defined in extension modules have type
types.BuiltinFunctionType, and have no __get__ method. Adding them as
attributes to classes and calling them through an instance of the
class does not result in them being called as methods: self is lost.

What's the simplest way of getting around this ?

By trial and error, I seem to have found that passing None as the
second argument (the instance) to new.instancemethod, does the trick.

The library reference manual confirms that it is actually supposed to
work this way. It's a pity that the builtin documentation does not:

Help on built-in function instancemethod:

instancemethod(...)
Create a instance method object from (FUNCTION, INSTANCE, CLASS).
(END)
 
M

Michael Hudson

Jacek Generowicz said:
By trial and error, I seem to have found that passing None as the
second argument (the instance) to new.instancemethod, does the trick.

PyDescr_NewMethod from C maybe?
The library reference manual confirms that it is actually supposed to
work this way. It's a pity that the builtin documentation does not:

The patch manager is over there --->

Cheers,
mwh

--
> With Python you can start a thread, but you can't stop it. Sorry.
> You'll have to wait until reaches the end of execution.
So, just the same as c.l.py, then?
-- Cliff Wells & Steve Holden, comp.lang.python
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top