On Wed, Jul 6, 2016 at 4:56 PM, Alex Hall <ah...@autodist.com> wrote:
> > > On Wed, Jul 6, 2016 at 3:35 PM, bruce <badoug...@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! >> > > I thought of an example that may help. It's not a great example, but here goes. Say you own a bakery, and you employ Joe and me. You task me with taking cookies from the kitchen and putting them in the display case in the service area, which I do well. The problem is that I don't know what looks appealing and what doesn't, so I keep putting cookies out that don't look great. You can't teach something like that, it's just something you know. Since you can't modify me to do my task in the way you want, you grab Joe and have him help me. Instead of putting all the cookies out, I now have Joe checking me before I place each one. Joe knows appealing, so is good at this, but he's also usually busy up front so can't carry the cookies out from the kitchen like I can. Joe is the decorator, and I'm the function being decorated. Since I do my task well, but lack a component you want, and since that component would be hard to program, you find an existing version of the component (Joe) and tell us to work together. Joe can modify my output before I return it, letting him filter the appealing cookies out while I do the work of carrying them up and setting out the ones Joe doesn't tell me to reject. Hopefully this makes some sense. > 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@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Alex Hall > Automatic Distributors, IT department > ah...@autodist.com > -- Alex Hall Automatic Distributors, IT department ah...@autodist.com _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor