[Tutor] Writing decorators?

Alex Hall ahall at autodist.com
Tue Jul 5 09:22:52 EDT 2016

Hi list,
I've read three or four articles on creating decorators. I don't know why,
but the use of decorators makes sense, yet the creation of them isn't
clicking. I get the idea, but when it comes to precisely how to write
one--what goes where and gets returned/called when--it's just not making
complete sense.

To simplify things, what might be an example of a decorator that, say,
prints "decorated" before whatever string the decorated function prints?
That is:

def printSomething(str):
    print str

printSomething("Hello world.")
#result should be:
decoratedHello world.

My attempt would be:

def prependDecorated(f):
  def prepend():
    return "decorated"+f()
  #something should go at this level too?

As I said, I've already read several sources on this. Are there any that
people find especially clear and helpful? Thanks for any links and/or

Alex Hall
Automatic Distributors, IT department
ahall at autodist.com

More information about the Tutor mailing list