[Tutor] decorators -- treat me like i'm 6.. what are they.. why are they?

Alex Hall ahall at autodist.com
Wed Jul 6 16:56:47 EDT 2016


On Wed, Jul 6, 2016 at 3:35 PM, bruce <badouglas at gmail.com> wrote:

> Hi.
>
> Saw the decorator thread earlier.. didn't want to pollute it. I know, I
> could google!
>
> But, what are decorators, why are decorators? who decided you needed them!
>

They're functions that modify the decorated function. If I make a function
that performs a task, I might decorate with a logging function:

@logThis
def doSomething():
  #do stuff

The logThis decorator could log the run, or modify the doSomething output,
or do any number of tasks. To get that extra functionality, I need only
decorate my own function. I see these a lot in Flask, a web framework,
where they are used to define what parts of your website are handled by
what functions, for instance. Basically, they let you extend what your
functions do without needing to subclass anything.

>
> Thanks!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



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


More information about the Tutor mailing list