Hooks (Re: What does Python fix?)

taranik taranik_at_intertax_dot_ru at foo.com
Wed Jan 23 23:30:50 EST 2002


Pedro Rodriguez wrote:

> "Hung Jung Lu" <hungjunglu at yahoo.com> wrote:

> <snip>
> > What doesn't Python fix? Hmm... as far as hooks are concerned, I often
> > wish there were a simpler way of inserting hooks into pre-existing
> > functions/methods more easily. That is, given a function/method f(), it
> > would be nice to be able to insert pre-functions and post-functions.
> > Mathematically, this is replacing a function f() with a function
> > composition g(f(h())) or g*f*h(). Function composition is not easy in
> > Python. It can be done, it's just not easy. Why do we need function
> > composition? Again, the matter is hooks and code-reuse. In all the
> > languages I know, dynamic function composition is not easy, and frankly,
> > in Java/C++ it seems totally imposibble. (Security of course is a huge
> > concern, but let us not get into there.) Yet function composition is a
> > powerful tool. For instance, a typical situation would be something like
> > the Apache mod_rewrite module: given a URL, it originally was supposed
> > to retrieve a certain web object. However, if you can insert hooks, you
> > can modify the URL just in time, and retrieve a new object somewhere
> > else. Function composition also allows people to do thing like
> > profiling: how many times a function has been called, how much time is
> > spent in a function, etc. Function composition allows the implementation
> > of security: when a function is called, you can check the authorization
> > at that very last microsecond, hence protecting at the individual
> > object/method level. Function composition of course also allows easier
> > implementation of patches: a third-party package can be more easily
> > patched, without the need of modifying the third-party's source code.
> > Dynamically changing the code of a function (in a general way) is not
> > easy nor do I think it is too widely useful, BUT, dynamically
> > pre-inserting and post-inserting hooks does seem to me to be a
> > personally-often-wished feature, in my experience.
> > 
> > Sorry for not being too detailed. But I hope the general idea did get
> > through. :)
> > 
> > regards,
> > 
> > Hung Jung

> Have you looked at what Aspect Oriented design aims at ? I recently
> posted two messages on this topic, because I came accross the same
> things that your are describing. Your function composition is
> refered as an 'aspect'.

> function composition : in one of the first example of aspects,
> you realize that thinking of function composition can lead to
> optimization. For instance imagine that, in your example, f(g(h())),
> f = g = h = MatrixAdd, this leads to an inefficient code, where
> temporary matrices have to be allocated, and you will loop 3 times
> accross the matrix. But if you can compute f*g*h prior to doing
> the actual computation, you will gain on both sides (speed/memory).
> You can deal with this kind of problem with lazy evaluations or
> monads.

> Pre/Post/Around methods : I posted a simple implementation for this, 
> à la AspectJ (Java) and AspectR (Ruby). And comparing with AspectJ,
> I am sure Python can provide that kind of implementation in simple
> and elegant way, in comparison with the compiler and language
> modification AspectJ introduces.

> This is an example of a patch to the current problem in Python 2.2
> in regard with ftplib/urllib (ftp passive mode not being the default)

> # ----------------------------------------------------------------------
> import aspect

> class Patch(aspect.Aspect):
>     def after_ftp_init(self, method, methodName, inst, *args, **kwargs):
>         inst.passiveserver = 1


> import ftplib

> patch = Patch()
> patch.wrap_after("after_ftp_init", ftplib.FTP, "__init__")
> # ----------------------------------------------------------------------


> Regards,
> -- 

> Pedro






##-----------------------------------------------##
         Message Posted With MyNewsGroups :)
         http://mynewsgroups.sourceforge.net
         Share your Knowledge in... comp.lang.python
##-----------------------------------------------##



More information about the Python-list mailing list