[Python-Dev] Re: Another approach to decorators.

David Abrahams dave at boost-consulting.com
Thu Aug 12 20:07:45 CEST 2004


Jp Calderone <exarkun at divmod.com> writes:

> David Eppstein wrote:
>> In article <un011l2qz.fsf at boost-consulting.com>,
>>  David Abrahams <dave at boost-consulting.com> wrote:
>> 
>>>>class Foo:
>>>>
>>>>  decorate static:
>>>>
>>>>    def static1(blah...):
>>>>        pass
>>>>
>>>>    decorate locksFile:
>>>>
>>>>      def static2andLocks(blah...):  # both decorators appy
>>>>          pass
>>>
>>>Wow, Martin Z's idea (using your keyword) really went "thunk" for me.
>>>What decorate does would be very much like what "class" does in some
>>>ways.
>> class: (and other something: constructs) start a block that can
>> contain any code.  Does this decorate keyword allow e.g. assignments
>> as well as defs and other decorates?  Or loops?  If so what should
>> it mean?  Is it like that locals() gets replaced by a special
>> dictionary-like-object that calls the decorator whenever any of its
>> contents gets set?
>> 
>
>    (I have no idea what the original poster intended, however....)
>
>      'decorate' expression ':'
>          suite
>
>    could create a nested scope, the locals of which could be passed to
>    whatever "expression" evaluates to (or if it is a tuple, do the
>    looping thing, since people seem to like that).
>
>    The call can return a dict with which the class dict is updated.

That's almost exactly what I intended.


<snip>

>    This covers a use case James Knight mentioned as well.  As he
>    pointed out, '@' and other proposals only decorate functions (and
>    maybe classes, soon).  The example he gave, I believe, was:
>
>      @public
>      x = 1
>
>    This doesn't work, but:
>
>      public:
>          x = 1
>
>    would.  Now, swallow that scream of horror (I did, barely
>    ;). 

No screams; I love it.


-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com



More information about the Python-Dev mailing list