On Thu, 22 Mar 2007 at 09:38, Ed Singleton wrote:
> I did something similar myself in a different thread, which might help.
>
> http://groups.google.com/group/turbogears/browse_thread/thread/2a9cca20dff1f10c
Thanks, I bookmarked that in case I need it later, but just using a
callable for the form argument to @validate worked for what I needed
to do.
> There's also a thread thread where Alberto taught me about im_func,
> which I still don't understand but seems to be important for this kind
> of thing.
Here's my understanding of it, which could be flawed :)
What is stored in 'self.index' is a descriptor which defines
a function (index) bound to an object (self). <descriptor>.im_func
is a pointer to the actual function object (the 'unbound method').
(If bound a function gets self as its first arg, if not bound the
first arg you pass it goes into the first arg in its signature
whether that arg is called self or not).
Here's what it looks like when you use @error_handler in a class:
class foo:
def bar(self): pass
@error_handler(bar)
def bim(self): pass
Now, at that point you don't have an instance. What you've done is
defined a function 'bar' in the class name space. In the class namespace
bar is still an unbound method. It doesn't get bound until you reference
it through the instance object.
So when you are creating the function in your __init__ method you
need to get access to the original, unbound version of 'bar', because
that's what @error_handler is expecting to be passed. You do that via
self.bar.im_func.
--David
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---