Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'example:': 0.03; 'class,': 0.07; 'suppose': 0.07; 'subject:help': 0.08; 'assuming': 0.09; 'decorator': 0.09; 'run,': 0.09; "they've": 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'argument.': 0.16; 'attribute"': 0.16; 'called,': 0.16; 'called.': 0.16; 'does,': 0.16; 'normally,': 0.16; 'self.y': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'trying': 0.19; 'thorough': 0.19; 'meant': 0.20; 'command': 0.22; 'cc:addr:python.org': 0.22; 'decorators': 0.24; 'fairly': 0.24; 'helpful': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'second': 0.26; 'header:In- Reply-To:1': 0.27; 'point': 0.28; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; "d'aprano": 0.31; 'once,': 0.31; 'spirit': 0.31; 'steven': 0.31; 'class': 0.32; 'probably': 0.32; 'regular': 0.32; 'skip:_ 10': 0.34; 'classes': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'functions.': 0.36; 'keyword': 0.36; 'method': 0.36; "i'll": 0.36; 'too': 0.37; 'jason': 0.38; 'pm,': 0.38; 'does': 0.39; 'how': 0.40; 'even': 0.60; "you're": 0.61; 'such': 0.63; 'provide': 0.64; 'more': 0.64; 'worth': 0.66; 'here': 0.66; 'mar': 0.68; 'other.': 0.75; 'potentially': 0.81; "'class'": 0.84; ':).': 0.84; 'toy': 0.84; '\xa0but': 0.84; 'to:none': 0.92; 'responses': 0.93; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:cc:content-type; bh=XGHFESiSacoU61zNEdI8WBBBMM0KQHjBuxvm4FuN9zs=; b=dPKOGAtZpSHglE2h11HaVcz2cl6+KU9BEkri52C6Ye4Y7+8WrG1i0HvSwOy5MmSiVF f2BfGHqWnIxmga8cq1SC+C5uoU+dYKL3F0O5U5mc00rd+yvHGM4kC0RXYCCYfcCmTxlD o1DL8ycUCDxTF6EmH3R6JvB/UIFj7dSGL1GCJsUjwRr+orCEP7y+4MTgxa6UVvsSQSrP i9kqcbWfPstrd7mxU6Qjqn1Yv/nSxih/Yz29bvMd8rw2EkTLrNR+nsjMBu/YjM71BDyd +u7QzJ9lgXvSohCkLV9qQR+RNd3AJBwIVaNJFrEAXWTkQVUbDO+XZmLuhSuihKLI/K0+ i9Pg== MIME-Version: 1.0 X-Received: by 10.50.114.37 with SMTP id jd5mr6263869igb.2.1364438292108; Wed, 27 Mar 2013 19:38:12 -0700 (PDT) In-Reply-To: <515380f4$0$29998$c3e8da3$5496439d@news.astraweb.com> References: <515380f4$0$29998$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 27 Mar 2013 22:38:11 -0400 Subject: Re: Decorator help From: Jason Swails Cc: python-list@python.org Content-Type: multipart/alternative; boundary=047d7b3a98acb81a4d04d8f30bc6 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364438295 news.xs4all.nl 6933 [2001:888:2000:d::a6]:44672 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42068 --047d7b3a98acb81a4d04d8f30bc6 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Mar 27, 2013 at 7:29 PM, Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > > The one doesn't follow from the other. Writing decorators as classes is > fairly unusual. Normally, they will be regular functions. If your > decorator needs to store so much state that it needs to be a class, > you're probably trying to do too much from a single decorator. > > There's more that you need to describe, such as what it is that the > decorator actually does, and whether it does it once, when the decorator > is called, or repeatedly, when the decorated method is called. > > The second case is the easiest. Suppose you have a class like this, with > many methods which have code in common. Here's a toy example: > > > def MyClass(object): > x = "class attribute" > > def __init__(self, y): > self.y = y > In the spirit of nit-picking, I'll point out that Steven meant to use the 'class' keyword instead of 'def' for MyClass. > def MyClass(object): > x = "class attribute" > > def __init__(self, y): > self.y = y > And here as well. It's potentially worth pointing out that this code will actually compile. It will even run, assuming you provide MyClass with a single argument. But it will always return None :). As per usual, the response was thorough and helpful -- I appreciate responses like these and how they've helped improve my command of Python. All the best, Jason --047d7b3a98acb81a4d04d8f30bc6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

On Wed, M= ar 27, 2013 at 7:29 PM, Steven D'Aprano <steve+co= mp.lang.python@pearwood.info> wrote:

The one doesn= 9;t follow from the other. Writing decorators as classes is
fairly unusual. Normally, they will be regular functions. If your
decorator needs to store so much state that it needs to be a class,
you're probably trying to do too much from a single decorator.

There's more that you need to describe, such as what it is that the
decorator actually does, and whether it does it once, when the decorator is called, or repeatedly, when the decorated method is called.

The second case is the easiest. Suppose you have a class like this, with many methods which have code in common. Here's a toy example:


def MyClass(object):
=A0 =A0 x =3D "class attribute"

=A0 =A0 def __init__(self, y):
=A0 =A0 =A0 =A0 self.y =3D y

In the spi= rit of nit-picking, I'll point out that Steven meant to use the 'cl= ass' keyword instead of 'def' for MyClass.
=A0
<= blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px= #ccc solid;padding-left:1ex"> def MyClass(object):
=A0 =A0 x =3D "class attribute"

=A0 =A0 def __init__(self, y):
=A0 =A0 =A0 =A0 self.y =3D y

And here a= s well.

It's potentially worth pointing out th= at this code will actually compile. =A0It will even run, assuming you provi= de MyClass with a single argument. =A0But it will always return None :).=A0=

As per usual, the response was thorough and helpful -- = I appreciate responses like these and how they've helped improve my com= mand of Python.

All the best,
Jason
--047d7b3a98acb81a4d04d8f30bc6--