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


Groups > comp.lang.python > #42068

Re: Decorator help

References <mailman.3824.1364413867.2939.python-list@python.org> <515380f4$0$29998$c3e8da3$5496439d@news.astraweb.com>
Date 2013-03-27 22:38 -0400
Subject Re: Decorator help
From Jason Swails <jason.swails@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3841.1364438295.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

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


Thread

Decorator help "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-03-27 19:49 +0000
  Re: Decorator help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-27 23:29 +0000
    Re: Decorator help Jason Swails <jason.swails@gmail.com> - 2013-03-27 22:38 -0400
      Re: Decorator help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-28 04:58 +0000
    RE: Decorator help "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-03-31 00:06 +0000

csiph-web