RE: [Zope] Override a method of an instance in the ZODB?
From: zope-bounces@zope.org [mailto:zope-bounces@zope.org] On Behalf Of Alec Mitchell
Which won't acquire anything but the method. The reason I want to avoid a subclass or monkey patch is that I want to override what this method does for this one instance of an already populated acl_users without changing it in any other way.
In Python, you can override a method of an instance just by assigning it to another function, like this - class Test1: def f1(self): return "f1() of Test1" def f1(): return "f1() of instance" T1 = Test1() T1.f1 = f1 Running this within the Python interpreter, try the following -
T1.f1() 'f1() of instance'
Is this what you had in mind? Cheers, Tom P
On Thursday 27 May 2004 02:00 pm, Passin, Tom wrote:
In Python, you can override a method of an instance just by assigning it to another function, like this -
class Test1: def f1(self): return "f1() of Test1"
def f1(): return "f1() of instance"
T1 = Test1() T1.f1 = f1
Running this within the Python interpreter, try the following -
T1.f1()
'f1() of instance'
Is this what you had in mind?
That's the idea, this is essentially what I did in my original post, except that your demo creates a function and not a bound method (e.g. it doesn't automatically make the instance the first argument). I initially tried this, which failed because it wasn't really a method and had no way of obtaining its context. Then I learned how to convert a function into an instance method in python, which works great until the transaction commits to the ZODB. Unfortunately, the ZODB won't store the method because it's 'unpicklable', the same is true for a plain function. I vaguely recall being able to do this successfully in an earlier version of Zope (<=2.6.3, using python 2.1), though it was something of an accident and ended up causing a few problems (all of which were entirely my fault). It seems strange that the ZODB would subsequently have lost this ability. Thanks, Alec Mitchell
On Thursday 27 May 2004 02:17 pm, Alec Mitchell wrote:
On Thursday 27 May 2004 02:00 pm, Passin, Tom wrote:
In Python, you can override a method of an instance just by assigning it to another function, like this -
class Test1: def f1(self): return "f1() of Test1"
def f1(): return "f1() of instance"
T1 = Test1() T1.f1 = f1
Running this within the Python interpreter, try the following -
T1.f1()
'f1() of instance'
Is this what you had in mind? After a little more digging it turns out that this (a normal function) will get stored in the ZODB without errors (as my past experience would indicate). When I first tested it, I used a lambda function instead of a normal function definition out of laziness, and the lambda apparently won't pickle into the ZODB, though a normal function will. So, fortunately, nothing seems to have changed between Python/Zope versions in this regard. However, that still leaves the problem that doing this doesn't create a bound method, but a simple function, which cannot properly override the original method (because it lacks context). Perhaps I should ask about this on the ZODB list?
Alec Mitchell
participants (2)
-
Alec Mitchell -
Passin, Tom