Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72891

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

From Ben Finney <ben@benfinney.id.au>
Subject Re: Decorating one method of a class C with another method of class C?
Date 2014-06-07 10:28 +1000
References <CAGGBd_pM5DGp85LVfeQgYwqS8DU8nuQL8Q8G5EzfQ5BX26fa-w@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.10838.1402100900.18130.python-list@python.org> (permalink)

Show all headers | View raw


Dan Stromberg <drsalists@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Decorating one method of a class C with another method of class C? Ben Finney <ben@benfinney.id.au> - 2014-06-07 10:28 +1000

csiph-web