Using descriptors

Andróg i Ngaurwaith adurdin at yahoo.com
Thu Oct 31 22:31:40 EST 2002


Mike Fletcher <mcfletch at rogers.com> wrote in message news:<cAUv9.119528$Q3S.60747 at news01.bloor.is.net.cable.rogers.com>...
> You haven't, in your example, used any descriptors (explicitly).  A 
> descriptor would (for instance) automatically wrap whatever was stored 
> in "testMethod" with the appropriate pre and post conditions.  For 
> instance, a staticmethod descriptor wraps a function for access such 
> that the function is returned un-changed, a classmethod wraps a function 
> for access such that the function is passed a pointer to the class when 
> called, etceteras.

Hmm... I don't really understand the distinction. What is it about
descriptors that make them different from other classes (such as I
used in the example)? To aid my understanding, would it be possible
for you to give a definition for or some code illustrating this
"wrapping"?

> Assume we have a hypothetical pre and post condition descriptor class:
*snip*
> Now, the descriptor would automatically wrap the methods (do_x, do_y) 
> with a PrePostMethod wrapper (which would take 3 args, pre, post and 
> method) when they were accessed as O().x or O().y, and they would do 
> that wrapping _even for new methods/functions/instances_ if you include 
> set functionality in your "prepostmethod" descriptor class.

I may sound dense, but I really don't understand what you're getting
at here with "they would do that wrapping _even for new
methods/functions/instances_" I can't make any sense out of this
explanation.

> Realistically, though, pre and post conditions for _methods_ are 
> probably best done with asserts at the start and end of the method-body; 
> far less overhead at run-time.

Pre- and post-conditions are not actually what I'm after here; I'm
trying to implement a way of transparently getting information (method
name, parameters) about some method calls (for uses such as logging,
etc), without having to add the code into the method.

Andrew



More information about the Python-list mailing list