Python Forum
what is this doc page for
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is this doc page for
#1
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.
Reply
#2
Looks like a bunch of decorator examples, constructed in a non-dynamic way.
Reply
#3
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
Reply
#4
(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.
Reply
#5
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
Reply
#6
(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.
Reply
#7
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:

>>> 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:
>>> @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.
Reply
#8
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.
Reply
#9
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:
@app.route('/login', methods=['GET', 'POST'])
def login():
   .....
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
def hello(count, name):
   .....
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020