I'm currently using code like this to create unbound methods
from functions and stick them into classes:
method = new.instancemethod(raw_func, None, cls)
setattr(cls, name, method)
Ok, python 2.6, run with the -3 flag, gives a warning that the new
module is going away in python 3.0, so the equivalent code is:
method = types.MethodType(raw_func, None, cls)
setattr(cls, name, method)
However, this code will not work in Python 3.0 because there are
no unbound methods any longer. The only way that I found so far
is this code:
method = lambda self, *args: raw_func(self, *args)
setattr(cls, name, method)
or the equivalent:
def method(self, *args):
return raw_func(self, *args)
setattr(cls, name, method)
but this is very ugly, imo. Is there another way?
The raw_func instances that I have are not descriptors (they
do not implement a __get__() method...)
Thanks,
Thomas 6 1750
Thomas Heller wrote:
but this is very ugly, imo. Is there another way?
The raw_func instances that I have are not descriptors (they
do not implement a __get__() method...)
I've written PyInstanceMethod_Type for this use case. It's not (yet)
available for Python code. Barry hasn't decided whether he should expose
the type so late in the release cycle or not. See http://bugs.python.org/issue3787 and http://docs.python.org/dev/3.0/c-api...nceMethod_Type
Christian
Christian Heimes schrieb:
Thomas Heller wrote:
>but this is very ugly, imo. Is there another way? The raw_func instances that I have are not descriptors (they do not implement a __get__() method...)
I've written PyInstanceMethod_Type for this use case. It's not (yet)
available for Python code. Barry hasn't decided whether he should expose
the type so late in the release cycle or not. See http://bugs.python.org/issue3787 and http://docs.python.org/dev/3.0/c-api...nceMethod_Type
Ok, so one has to write an extension to access or expose it.
Oh, wait - there's ctypes:
Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>from ctypes import * pythonapi.PyInstanceMethod_New.restype = py_object pythonapi.PyInstanceMethod_New.argtypes = [py_object] instancemethod = pythonapi.PyInstanceMethod_New
class Example:
.... pass
....
>>Example.id = instancemethod(id)
x = Example() x.id()
12597296
>>id(x)
12597296
>>>
Thomas
Thomas Heller wrote:
Christian Heimes schrieb:
>Thomas Heller wrote:
>>but this is very ugly, imo. Is there another way? The raw_func instances that I have are not descriptors (they do not implement a __get__() method...)
I've written PyInstanceMethod_Type for this use case. It's not (yet) available for Python code. Barry hasn't decided whether he should expose the type so late in the release cycle or not. See http://bugs.python.org/issue3787 and http://docs.python.org/dev/3.0/c-api...nceMethod_Type
Ok, so one has to write an extension to access or expose it.
Oh, wait - there's ctypes:
Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>from ctypes import * pythonapi.PyInstanceMethod_New.restype = py_object pythonapi.PyInstanceMethod_New.argtypes = [py_object] instancemethod = pythonapi.PyInstanceMethod_New
class Example:
... pass
...
>>>Example.id = instancemethod(id)
x = Example() x.id()
12597296
>>>id(x)
12597296
A pyCapi module that exposed via ctypes useful C functions not otherwise
accessible, with predefinition of restype and argtypes and anything else
needed, might make a nice addition to PyPI if not the stdlib. You have
done two here and I believe others have posted others.
tjr
Terry Reedy schrieb:
Thomas Heller wrote:
>Christian Heimes schrieb:
>>I've written PyInstanceMethod_Type for this use case. It's not (yet) available for Python code.>Oh, wait - there's ctypes:
Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>>>from ctypes import * pythonapi.PyInstanceMethod_New.restype = py_object pythonapi.PyInstanceMethod_New.argtypes = [py_object] instancemethod = pythonapi.PyInstanceMethod_New > class Example:
... pass ...
>>>>Example.id = instancemethod(id) > x = Example() x.id()
12597296
>>>>id(x)
12597296
A pyCapi module that exposed via ctypes useful C functions not otherwise
accessible, with predefinition of restype and argtypes and anything else
needed, might make a nice addition to PyPI if not the stdlib. You have
done two here and I believe others have posted others.
Well, Lenard Lindstrom has some time ago contributed a module like that
which is available in the (more or less unmaintained) ctypeslib project: http://svn.python.org/projects/ctype...b/pythonhdr.py
However, it was probably more meant to provide a complete Python C api,
instead of concentrating on stuff not available to Python otherwise.
Thomas
Thomas Heller wrote:
Ok, so one has to write an extension to access or expose it.
Oh, wait - there's ctypes:
I wrote the type to help the Pyrex and Cython developers to port their
software to 3.0. I planed to expose the type as
__builtin__.instancemethod but forgot it. Maybe we can convince Barry
together. He still considers my bug as a release blocker for 3.0.
Christian
Christian Heimes schrieb:
Thomas Heller wrote:
>Ok, so one has to write an extension to access or expose it.
Oh, wait - there's ctypes:
I wrote the type to help the Pyrex and Cython developers to port their
software to 3.0. I planed to expose the type as
__builtin__.instancemethod but forgot it. Maybe we can convince Barry
together. He still considers my bug as a release blocker for 3.0.
Issue 3787 is marked as release blocker, but for 3.1 IIUC. Would it help
if I post my use case to the tracker?
Thomas This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ben |
last post by:
Hi,
I was recently writing a python interpreter (in python), partly to teach
myself more about the python object system, and partly to learn more about
how programming languages work in general....
|
by: ZRexRider |
last post by:
Hi,
I have a report driven by a SQL database. I have a populated recordset
having a user name and a number of data/time fields. I process these
time fields in the report's Detail_Print event. ...
|
by: Ron Garret |
last post by:
If I do this:
def f(self): print self
class c1: pass
setattr(c1, 'm1', f)
Then f is automagically transmogrified into the appropriate sort of
method depending on how it is used:
|
by: Kevin Walzer |
last post by:
I am trying to structure a Tkinter application with classes instead of
just with simple functions, but I'm not sure how to call methods from my
main class.
My main class is packetstreamApp()....
|
by: 7stud |
last post by:
Here is some example code that produces an error:
class Test(object):
def greet():
print "Hello"
t = Test()
t.greet()
TypeError: greet() takes no arguments (1 given)
| |
by: Sverker Nilsson |
last post by:
do i dare to open a thread about this?
come on you braver men
we are at least not bought by g***le
but why? others have said it so many times i think
:-////
|
by: Aaron Watters |
last post by:
Why is the migration to py3k a concern?
For example I have libraries which use string%dictionary
substitution where the dictionary is actually an object
which emulates a dictionary. The...
|
by: DeZZar |
last post by:
Hi all,
Unfortunately I am quite a novice with Access!! I've created a number
of data bases for my work however becuase my skills are limited to
really built in functionality and wizards my...
|
by: Mohed |
last post by:
Hello.
I am interrested in seeing the source code for all the nativ builtin
methods and clases in python. Is there a webpage that lists them or is
this done easier some other way.
This is my...
|
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,...
|
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,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |