Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is python @decorator related to the decorator design pattern?

I might seem stupid here. However, I was writing some python code, and this thing struck me. In python there are things called decorators which are denoted by @ and used "around" functions like:

class PlotABC(metaclass=ABCMeta):

  @property
  def figure(self):
    if self._figure is None: self.__create_figure()
    return self._figure

  @abstractmethod
  def _prepare_series(self):
    pass

I also know slivers of design patterns and I know there are patterns called decorators. Then once I thought, "Hey maybe the name similarity is not a bizarre coincidence."

I have read wiki: Decorator pattern and the great answer How to make a chain of function decorators? (almost whole).

It seems to me that python's decorator semantics are really powerful and can serve to implement the design pattern. As they allow to wrap around functions and methods. Though, I am still confused, since I am inexperienced with design patterns. I also do not know a language with such a dedicated mechanism for them.

Could someone experienced in python and design patterns say if the python semantics were purposely created to create decorator patterns? Is it something more, less, or does it mean something different?

And maybe throw some light how declaring a method abstract or a property is decorating it?

like image 420
luk32 Avatar asked Jul 29 '13 14:07

luk32


People also ask

Do design patterns apply to Python?

Yes, Python has design patterns. In fact, design patterns are simply ways to solve problems; they are independent of any programming language. You can implement most design patterns in a wide range of programming languages.

Did Python use decorators?

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.

What is the decorator pattern also known as?

A Decorator Pattern says that just "attach a flexible additional responsibilities to an object dynamically". In other words, The Decorator Pattern uses composition instead of inheritance to extend the functionality of an object at runtime. The Decorator Pattern is also known as Wrapper.

What type of design pattern is decorator?

Decorator design pattern is one of the structural design pattern (such as Adapter Pattern, Bridge Pattern, Composite Pattern) and uses abstract classes or interface with composition to implement.


2 Answers

No, it's not a bizarre coincidence, and no, python @decorators do not implement the GOF Decorator pattern.

The GOF (Gang of Four, so named after the four authors of the book "Design Patterns") decorator pattern adds "decorations" to change the behaviour of an object. It does this by creating "decorator" classes that can be applied to undecorated base classes.

Python decorators adds a "decoration" to a function or a class. These decorators are essentially functions that wrap or modify the function/class.

Both are similar in that you can re-use the decorator to add the same functionality several times, but the decorator pattern requires the decorators to be designed together with and for the classes that you can decorate.

Python decorators instead just do their work on any class of function.

like image 148
Lennart Regebro Avatar answered Oct 24 '22 13:10

Lennart Regebro


A "pattern" is a workaround for a missing language feature. "Decoration" is a term which pre-existed the Gang Of Four patterns book in programming - it has been used to describe any number of operations that involve adding some information or behaviour onto something else.

Strictly speaking, this isn't a way of implementing the decorator pattern, because it doesn't involve the creation of a class which composes other classes (except co-incidentally that that is one thing you can do with a decorator). It does, however, together with python's other features, render it largely obsolete.

In fact, I'd add that most "patterns" are a way of faking dynamic or reflective behaviour in C++, Java, and the like. You'll find very little excitement outside of those language communities. In Python decorators and metaclasses provide a way to encode all kinds of transformations - I'd guess that a majority of patterns which aren't trivial code be substantially replaced with specific decorators or metaclasses in Python.

like image 22
Marcin Avatar answered Oct 24 '22 12:10

Marcin