Re: Dynamic class methods misunderstanding
From: Christos (tzot_at_sil-tec.gr)
Date: 01/31/05
- Next message: Roger Upole: "Re: COM on a network drive can't find pywintypes23.dll"
- Previous message: Nick Coghlan: "Re: Nested scopes and class variables"
- In reply to: Alex Martelli: "Re: Dynamic class methods misunderstanding"
- Next in thread: Alex Martelli: "Re: Dynamic class methods misunderstanding"
- Reply: Alex Martelli: "Re: Dynamic class methods misunderstanding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 31 Jan 2005 13:01:42 +0200
On Sat, 29 Jan 2005 10:24:27 +0100, rumours say that aleaxit@yahoo.com
(Alex Martelli) might have written:
>Bill Mill <bill.mill@gmail.com> wrote:
> ...
>> > class Test:
>> > def __init__(self, method):
>> > self.m = new.instancemethod(method, self, Test)
>>
>> Beautiful! thank you very much. Looking into the "new" module in
>> python 2.4, that's equivalent to:
>>
>> self.m = type(self.__init__)(method, self, Test)
>Another approach with the same result is to exploit the fact that a
>function is a descriptor:
>
>self.m = method.__get__(self, Test)
Almost true; not all builtin functions are descriptors though.
.>> import new
.>> f= new.instancemethod(divmod, 7, object)
.>> map(f, range(1,10,2))
[(7, 0), (2, 1), (1, 2), (1, 0), (0, 7)]
.>> f= divmod.__get__(7)
Traceback (most recent call last):
File "<pyshell#16>", line 1, in -toplevel-
f= divmod.__get__(7)
AttributeError: 'builtin_function_or_method' object has no attribute
'__get__'
I didn't run an extensive test, but it's possible that all builtin
functions are not descriptors.
-- TZOTZIOY, I speak England very best. "Be strict when sending and tolerant when receiving." (from RFC1958) I really should keep that in mind when talking with people, actually...
- Next message: Roger Upole: "Re: COM on a network drive can't find pywintypes23.dll"
- Previous message: Nick Coghlan: "Re: Nested scopes and class variables"
- In reply to: Alex Martelli: "Re: Dynamic class methods misunderstanding"
- Next in thread: Alex Martelli: "Re: Dynamic class methods misunderstanding"
- Reply: Alex Martelli: "Re: Dynamic class methods misunderstanding"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]