[Freeipa-devel] more funky interface stuff
John Dennis
jdennis at redhat.com
Sat Dec 1 15:57:56 UTC 2007
Rob Crittenden wrote:
> John Dennis wrote:
>> Rob Crittenden wrote:
>>> Rob Crittenden wrote:
>> I think the vast majority of the code duplication in both ipaclient.py
>> and rpcclient.py can be eliminated with a single decorator, that would
>> be a huge step in simplification and consistency.
>
> Being new to python, I still really don't know what a decorator does (I
> haven't read the link you provided earlier yet). I've used it in
> TurboGears but much of that is still voodoo to me.
If I can dig up some time (an issue these days) I would be happy to
convert the code to use decorators. I've implemented RPC via decorators
in Python previously and could probably do it again pretty efficiently.
You're right, at first it seems like a bit of voodoo magic, but with an
understanding of function closure* (a very cool feature), the fact a
function is a full fledged object in Python and how function references
are handled it's not too bad. Unfortunately decorators are not well
documented and the first time I used decorators it took a while to wrap
my head around the concepts but when I got done I realized they're much
simpler and easy to use than one might first think.
* function closure can be seen in nested functions, it gives you the
ability to define a function within a run time scope, bind the variables
seen in that scope, let the outer function go out of scope (e.g.
return), but as long as you keep a reference to the nested function it
can be called and execute with the exact same variable bindings that
existed when it was defined. Very cool and very powerful. Asynchronous
method calls make a good example, you create an anonymous function
binding it as callback to an asynchronous RPC. When the RPC call returns
sometime in the future it executes the callback with the same variable
binding that existed when the asynchronous RPC was first called even
though that scope has vanished due to the fact the outer function has
since returned.
--
John Dennis <jdennis at redhat.com>
More information about the Freeipa-devel
mailing list