473,486 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Turning builtin functions into methods.

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.
Jul 18 '05 #1
2 1413
Jacek Generowicz <ja**************@cern.ch> writes:
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)
Jul 18 '05 #2
Jacek Generowicz <ja**************@cern.ch> writes:
Jacek Generowicz <ja**************@cern.ch> writes:
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.


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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1946
by: Blair Hall | last post by:
Can anyone please tell me how to correctly use a built in function when there is a function of the same name in local scope? Here is an example. Suppose the following is in myApply.py: def...
3
2898
by: Gonçalo Rodrigues | last post by:
Hi, Does anyone know if and how I can, from within Python, read the signatures of builtin methods/functions? The following fails: >>> import inspect >>> inspect.getargspec(list.append)...
10
4720
by: Stefan Seefeld | last post by:
hi there, I'm trying to convert a tuple to a list, and get a 'TypeError: list objects are unhashable'. Can anybody enlighten me as to the possible causes for this ? Where does hashing come...
0
1329
by: Joachim Dahl | last post by:
I would like to inherit the builtin array class and write som extension methods in C. Other people have suggested that I use numarray for that, but all I want is a simple continuous C array of...
20
8664
by: Brian Kazian | last post by:
Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using...
1
485
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
0
1652
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
20
2088
by: Ari Krupnik | last post by:
scripts can add methods to the prototypes of builtin objects in JaavScript. I can assign functions to String.prototype.*, for instance. I want to add a method to Node, but when I try to execute...
23
1979
by: =?ISO-8859-1?Q?Sch=FCle_Daniel?= | last post by:
Hello, lst = list((1,2,3)) lst = t = tupel((1,2,3)) t = (1,2,3) s = set((1,2,3)) s = ...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.