Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the difference between Python decorators and the decorator pattern?
    primarykey
    data
    text
    <p>What is the difference between “Python decorators” and the “decorator pattern”?</p> <p>When should I use Python decorators, and when should I use the decorator pattern?</p> <p>I'm looking for examples of Python decorators and the decorator pattern accomplishing same.</p> <p><strong>@AcceptedAnswer</strong></p> <p>I know that <a href="https://stackoverflow.com/a/8328867/708764">Jakob Bowyer's answer</a> is valid. Yet it's Strikar's answer that made me understand why.</p> <p>After Srikar's answer and studying given resources I've written this example, so I can visualize and understand Python decorators and the decorator pattern.</p> <p>I must <strong>disagree</strong> with Strikar's "<em>Python decorators are not an implementation of the decorator pattern</em>". After what I've learned <strong>I'm strongly convinced that Python decorators are implementation of the decorator pattern.</strong> Just not in the classic way.</p> <p><strong>Also</strong>, I need to add that despite the fact that Strikar said "<em>Python decorators add functionality to functions and methods at definition time</em>" <strong>you can easily use Python decorators at run time</strong>.</p> <p>Yet, I still mark Stiker's answer as accepted, because it helped me understand <a href="http://www.python.org/dev/peps/pep-0318/" rel="nofollow noreferrer"><strong>the implementation</strong></a> <strong>of the decorator pattern in Python</strong>.</p> <pre><code>""" Testing Python decorators against the decorator pattern """ def function(string): return string def decorator(wrapped): def wrap(string): # Assume that this is something useful return wrapped(string.upper()) return wrap def method_decorator(wrapped): def wrap(instance, string): # Assume that this is something useful return wrapped(instance, string.upper()) return wrap @decorator def decorated_function(string): print('! '.join(string.split(' '))) class Class(object): def __init__(self): pass def something_useful(self, string): return string class Decorator(object): def __init__(self, wrapped): self.wrapped = wrapped def something_useful(self, string): string = '! '.join(string.split(' ')) return self.wrapped().something_useful(string) @method_decorator def decorated_and_useful(self,string): return self.something_useful(string) if __name__ == '__main__': string = 'Lorem ipsum dolor sit amet.' print(function(string)) # Plain function print(decorator(function)(string)) # Python decorator at run time print(decorated_function(string)) # Python decorator at definition time a = Class() print(a.something_useful(string)) # Plain method b = Decorator(Class) print(b.something_useful(string)) # Decorator pattern print(b.decorated_and_useful(string)) # Python decorator decorated the decorator pattern </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload