Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #15564
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'instance,': 0.05; 'function,': 0.07; 'wrapper': 0.07; 'attribute.': 0.09; 'decorator': 0.09; 'func': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.14; '*args,': 0.16; 'args[0]': 0.16; 'received:washington.edu': 0.16; 'res': 0.16; 'subject:function': 0.16; 'ugly.': 0.16; 'unbound': 0.16; 'variable.': 0.16; 'subject:question': 0.17; 'instance': 0.18; 'trying': 0.21; 'thus': 0.23; 'works.': 0.23; "i'm": 0.26; 'tried': 0.28; 'bound': 0.28; 'work:': 0.28; 'second': 0.29; 'class': 0.29; 'disappointed': 0.30; 'pretty': 0.32; "can't": 0.32; 'to:addr:python-list': 0.32; 'there': 0.33; 'header:User- Agent:1': 0.33; 'header:X-Complaints-To:1': 0.33; '(for': 0.35; 'but': 0.37; 'received:org': 0.37; 'called': 0.38; 'case': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'skip:_ 10': 0.40; 'did': 0.40; 'results': 0.61; 'subject:but': 0.67; 'works,': 0.68; 'subject:class': 0.84; 'timings': 0.84; 'russell': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | "Russell E. Owen" <rowen@uw.edu> |
| Subject | Decorator question: prefer class, but only function works |
| Date | Thu, 10 Nov 2011 13:52:17 -0800 |
| Organization | University of Washington |
| X-Gmane-NNTP-Posting-Host | b172-28-191-0.nat.washington.edu |
| User-Agent | MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2626.1320962112.27778.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1320962112 news.xs4all.nl 6962 [2001:888:2000:d::a6]:48543 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:15564 |
Show key headers only | View raw
I am trying to write a decorator that times an instance method and
writes the results to a class member variable. For example:
def timeMethod(func):
def wrapper(self, *args, **keyArgs):
t1 = time.time()
res = func(self, *args, **keyArgs)
duration = time.time() - t1
self.timings[func.__name__] = duration
return res
return wrapper
This works, but I'm not very happy with the way self.timings is obtained.
I first tried to write this as a class (for readability), and this did
NOT work:
class timeMethod(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **keyArgs):
t1 = time.time()
res = self.func(*args, **keyArgs)
duration = time.time() - t1
args[0].timings.set(self.func.__name__, duration)
return res
In the first case the wrapper is called as an unbound function, so it
works. But in the second case the wrapper is called as a bound method --
thus args[0] is not func's class instance, and I can't get to the
timings attribute.
Unfortunately they are both pretty ugly. Is there a cleaner way to
access the the instance of which func is a member? I was very
disappointed it was not available when timeMethod was
called/instantiated.
-- Russell
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Decorator question: prefer class, but only function works "Russell E. Owen" <rowen@uw.edu> - 2011-11-10 13:52 -0800
csiph-web