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?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.024
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'class,': 0.07; 'referring': 0.07; 'dan': 0.09; 'decorator': 0.09; 'instance.': 0.09; 'methods,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'argument.': 0.16; 'finney': 0.16; 'merely': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:class': 0.16; 'trying': 0.19; 'later': 0.20; 'seems': 0.21; 'example': 0.22; 'header:User- Agent:1': 0.23; 'specify': 0.24; 'class.': 0.26; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'specifically': 0.29; 'feature': 0.29; "doesn't": 0.30; 'apparently': 0.31; 'way?': 0.31; 'writes:': 0.31; 'class': 0.32; '(i.e.': 0.33; 'actual': 0.34; 'subject:with': 0.35; "can't": 0.35; 'there': 0.35; 'subject:one': 0.36; 'done': 0.36; 'method': 0.36; 'useful': 0.36; 'subject:?': 0.36; 'should': 0.36; 'expected': 0.38; 'ben': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'office': 0.60; 'simple,': 0.60; "you're": 0.61; 'first': 0.61; 'complete': 0.62; 'show': 0.63; 'such': 0.63; 'refer': 0.63; 'more': 0.64; 'hours': 0.66; 'between': 0.67; 'importantly,': 0.68; 'a.m.': 0.84; 'athens': 0.84; 'clearer': 0.84; 'improvement': 0.84; 'received:125': 0.84; 'whereas': 0.91; 'why?': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben@benfinney.id.au>
Subject Re: Decorating one method of a class C with another method of class C?
Date Sat, 07 Jun 2014 10:28:03 +1000
References <CAGGBd_pM5DGp85LVfeQgYwqS8DU8nuQL8Q8G5EzfQ5BX26fa-w@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:1wjT/1lxX6KXLrBOiDdmbx88bp4=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10838.1402100900.18130.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402100900 news.xs4all.nl 2887 [2001:888:2000:d::a6]:34068
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72891

Show key headers only | 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