Decorating one method of a class C with another method of class C?

Ben Finney ben at benfinney.id.au
Fri Jun 6 20:28:03 EDT 2014


Dan Stromberg <drsalists at gmail.com> writes:

> Is there a way of decorating method1 of class C using method2 of class
> C?

Can you give a concrete example (i.e. not merely hypothetical) where
this would be a useful feature (i.e. an actual improvement over the
absence of the feature), and why?

> It seems like there's a chicken-and-the-egg problem; the class doesn't
> seem to know what "self" is until later in execution so there's
> apparently no way to specify @self.method2 when def'ing method1.

You're referring specifically to instance methods, it seems. Right,
there's no instance *of* the class during the *definition* of the class,
so ‘self’ can't refer to such an instance.

Also, an instance method needs to get the instance as the first
parameter, whereas the decorator must accept the to-be-decorated
function as its argument.

So I don't see what you're trying to achieve, rather than the more
straightforward and clearer use of a decorator function which *isn't* a
method in the same class.

Can you show a Simple, Self-Contained Complete Example to show us what
you're trying to do? More importantly, why you think it should be done
this way?

-- 
 \        “Visitors are expected to complain at the office between the |
  `\                     hours of 9 and 11 a.m. daily.” —hotel, Athens |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list