Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
edited by
I want to know the difference between “Python decorators” and the “decorator pattern” and the scenario where I need to use Python decorators, and when I have to use the decorator pattern?
I'm looking for instances of Python decorators and the decorator pattern accomplishing the same.

1 Answer

0 votes
by (108k points)

Please be informed that a decorator Python in object-oriented programming, is a design pattern that enables the behavior to be added to an existent object dynamically. The decorator pattern can be applied to extend (decorate) the functionality of a particular object at run-time, separately from other cases of the same class, provided some preparation is done at design time.

Whereas if we talk about decorators in Python, they are not an implementation of the decorator pattern. The decorator pattern is a design pattern applied in statically typed object-oriented programming languages to provide the functionality to be added to objects at run time; Python decorators add functionality to functions and methods at definition time and therefore are a higher-level construct than decorator-pattern classes.

Therefore in Python, a decorator is any callable Python object that is applied to change a function, method, or class definition.

Browse Categories

...