Posts: 4,653
Threads: 1,496
Joined: Sep 2016
May-14-2017, 05:49 AM
(This post was last modified: May-14-2017, 05:49 AM by Skaperen.)
i ran across this page while reading about decorators, can someone tell me what it is for?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 12,039
Threads: 487
Joined: Sep 2016
Looks like a bunch of decorator examples, constructed in a non-dynamic way.
Posts: 687
Threads: 37
Joined: Sep 2016
May-14-2017, 03:11 PM
(This post was last modified: May-14-2017, 03:11 PM by Ofnuts.)
As written on the lid:
Quote:This page is meant to be a central repository of decorator code pieces
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
(May-14-2017, 03:11 PM)Ofnuts Wrote: As written on the lid:
Quote:This page is meant to be a central repository of decorator code pieces
what can these be used for? i am still trying to understand decorators. it looks like they shorten the code just a tiny bit, something that looks like it could have been more powerful if there had been a front-end macro system to re-generate code based on given code.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 12,039
Threads: 487
Joined: Sep 2016
May-15-2017, 05:03 AM
(This post was last modified: May-15-2017, 05:04 AM by Larz60+.)
I know you don't like to watch videos, but this is a good one by David Beazley from PyCon 2014
It's titles generators, but it really ought to include decorators in the title because a generous
portion is about them.
http://www.dabeaz.com/finalgenerator/index.html
Posts: 2,342
Threads: 62
Joined: Sep 2016
(May-15-2017, 03:15 AM)Skaperen Wrote: what can these be used for? They are syntax for wrapping a function definition at definition time. They're not for changing built-in / third party functions at runtime. They're logically equivalent to other forms of wrapping. The best example I've seen is caching / memoization, and it doesn't necessary make the code shorter, though it helps with separation of concerns and if the decorator is reusable then you can have less code.
Posts: 3,458
Threads: 101
Joined: Sep 2016
May-15-2017, 05:49 PM
(This post was last modified: May-15-2017, 05:50 PM by nilamo.)
Decorators are just functions that take a function as an argument, and return a new function.
A lot of their use comes from modifying arguments for the base function, or modifying the function's output. For example, let's say you have a function that returns a dict. You could write another function that takes the base function's output (a dict) as input, and returns a json-encoded version of the same thing. It might look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> def process(spam):
... return { "wrapped" : spam }
...
>>> def as_json(func):
... import json
... def wrapper( * args):
... output = func( * args)
... return json.dumps(output)
... return wrapper
...
>>> processor = as_json(process)
>>> processor( "eggs" )
'{"wrapped": "eggs"}'
|
Instead of processor = as_json(process) , you could alternatively define the function with as_json as a decorator:
1 2 3 4 5 6 |
>>> @as_json
... def process2(spam):
... return { "still wrapped" : spam }
...
>>> process2( "eggs" )
'{"still wrapped": "eggs"}'
|
The decorator does the same thing, no special magic added. It exists to make it easier to compose functions and help make your code clearer. As mentioned, another big usage is in memoization, where you keep track of input->output in a dict, and only actually call the function if you haven't seen the args before... which makes expensive operations much faster, since you don't even bother running the function after the first time.
Posts: 2,342
Threads: 62
Joined: Sep 2016
Logging, metrics, and monitoring are also potential uses of decorators.
Logging, for example if the function throws an exception.
Metrics, such as the amount of time the calls take.
Monitoring / instrumentation generally speaking, e.g. a decorator that sends an email (rather than just log to a file) when a crucial function throws an exception, runs too slowly, or even returns unusual values.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Two of my favorite Python program Flask and Click,use decorator heavily.
This make the user interface cleaner,
bye hiding away code that's not important for the task that user shall solve.
Eg:
1 2 3 |
@app .route( '/login' , methods = [ 'GET' , 'POST' ])
def login():
.....
|
1 2 3 4 |
@click .command()
@click .option( '--count' , default = 1 , help = 'Number of greetings.' )
def hello(count, name):
.....
|
|