Using 'apply' as a decorator, to define constants

alex23 wuwei23 at
Sat Aug 22 04:35:38 CEST 2009

Jonathan Gardner <jgard... at> wrote:
> This is brilliant. I am going to use this more often. I've all but
> given up on property() since defining "get_foo", "get_bar", etc... has
> been a pain and polluted the namespace.

Unfortunately I can't remember who I first learned it from - it was
definitely in a post to this group - otherwise all credit would be

> It's one of those functions that is easier to define than import.

And so obvious now :)

> If anyone reads the decorator and doesn't think "this thing below is
> defined as the result of this decorator function" then they don't
> understand decorators at all.

Well, it's not so much a question of the reader's intelligence as that
Python already has a readily identifiable assignment operator. Having
a second mechanism for assignment with no real similarity to the first
just adds cognitive friction to reading the code...not because the
reader doesn't understand what is happening, but because it's not
obvious _why_ this second form would have been chosen.

Nothing that couldn't be mitigated with a comment, I guess.

    # @apply used to prevent having to repeat references

That would work for me.

More information about the Python-list mailing list