[Tutor] basic decorator question

Alan Gauld alan.gauld at yahoo.co.uk
Thu Jul 27 02:25:00 EDT 2017

On 27/07/17 03:22, boB Stepp wrote:

> use them.  The idea of replacing a function with its decorated version
> sounds cool, but what types of problems would I want to use this
> approach on?

I'm sure others will have their own take on this but personally I view
them as primarily for building frameworks. For example in the standard
Python pantheon we use decorators to create properties and classmethods.

Web frameworks like Django use decorators extensively.

They are a way of embedding user code into our framework code without
the user having to know anything about how the framework does its magic.

And that is why I try not to use decorators for day to day code -
because they hide how the code works. Its similar with metaclasses,
they are immensely powerful but should be used sparingly because they
obfuscate how the code is working. And that, IMHO, is a bad thing.

So if you have some functionality that you want to wrap around a
set of functions in a standard way while at the same time not impinging
on the core logic of those functions then use a decorator. If you are
not sure why you are using them don't use them, just write an explicit
wrapper instead.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list