Questions tagged [decorator]

Decorator is an object-oriented design pattern that allows adding behavior to existing classes in a dynamic fashion. It is one of the Gang of Four's structural design patterns.

The Decorator pattern can be used to add new functionality to a class without changing it. Typically it is implemented by subclassing or delegating.

In Python, a decorator is a specific way of using a callable to change the behavior of a function or class.

In Python, decorators are specified in PEP318.

Decorator is one of the Gang of Four's structural , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

More information is available on Wikipedia.

4933 questions
3129
votes
21 answers

How do I make function decorators and chain them together?

How do I make two decorators in Python that would do the following? @make_bold @make_italic def say(): return "Hello" Calling say() should return: "Hello"
Imran
  • 87,203
  • 23
  • 98
  • 131
1621
votes
37 answers

Creating a singleton in Python

This question is not for the discussion of whether or not the singleton design pattern is desirable, is an anti-pattern, or for any religious wars, but to discuss how this pattern is best implemented in Python in such a way that is most pythonic. In…
theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
1333
votes
15 answers

How does the @property decorator work in Python?

I would like to understand how the built-in function property works. What confuses me is that property can also be used as a decorator, but it only takes arguments when used as a built-in function and not when used as a decorator. This example is…
ashim
  • 24,380
  • 29
  • 72
  • 96
959
votes
7 answers

What does functools.wraps do?

In a comment on this answer to another question, someone said that they weren't sure what functools.wraps was doing. So, I'm asking this question so that there will be a record of it on StackOverflow for future reference: what does functools.wraps…
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
690
votes
23 answers

Decorators with parameters?

I have a problem with the transfer of the variable insurance_mode by the decorator. I would do it by the following decorator statement: @execute_complete_reservation(True) def test_booking_gta_object(self): self.test_select_gta_object() but…
falek.marcin
  • 9,679
  • 9
  • 28
  • 33
528
votes
40 answers

Experimental decorators warning in TypeScript compilation

I receive the warning... Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option `to remove this warning. ... even though my compilerOptions in tsconfig.json have the…
bensiu
  • 24,660
  • 56
  • 77
  • 117
464
votes
13 answers

How do the Proxy, Decorator, Adapter, and Bridge Patterns differ?

I was looking at the Proxy Pattern, and to me it seems an awful lot like the Decorator, Adapter, and Bridge patterns. Am I misunderstanding something? What's the difference? Why would I use the Proxy pattern versus the others? How have you used them…
Charles Graham
  • 24,293
  • 14
  • 43
  • 56
369
votes
13 answers

What are some common uses for Python decorators?

While I like to think of myself as a reasonably competent Python coder, one aspect of the language I've never been able to grok is decorators. I know what they are (superficially), I've read tutorials, examples, questions on Stack Overflow, and I…
Dana
  • 32,083
  • 17
  • 62
  • 73
349
votes
20 answers

How to get method parameter names?

Given that a function a_method has been defined like def a_method(arg1, arg2): pass Starting from a_method itself, how can I get the argument names - for example, as a tuple of strings, like ("arg1", "arg2")?
Staale
  • 27,254
  • 23
  • 66
  • 85
251
votes
20 answers

Is there a decorator to simply cache function return values?

Consider the following: @property def name(self): if not hasattr(self, '_name'): # expensive calculation self._name = 1 + 1 return self._name I'm new, but I think the caching could be factored out into a decorator. Only I…
Tobias
  • 3,882
  • 2
  • 22
  • 25
245
votes
7 answers

How to create abstract properties in python abstract classes?

In the following code, I create a base abstract class Base. I want all the classes that inherit from Base to provide the name property, so I made this property an @abstractmethod. Then I created a subclass of Base, called Base_1, which is meant to…
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
237
votes
2 answers

What's the '@' (at symbol) in the Redux @connect decorator?

I am learning Redux with React and stumbled upon this code. I am not sure if it is Redux specific or not, but I have seen the following code snippet in one of the examples. @connect((state) => { return { key: state.a.b }; }) While the…
Salman
  • 9,299
  • 6
  • 40
  • 73
230
votes
5 answers

How to implement a typescript decorator?

TypeScript 1.5 now has decorators. Could someone provide a simple example demonstrating the proper way to implement a decorator and describe what the arguments in the possible valid decorator signatures mean? declare type ClassDecorator =
David Sherret
  • 101,669
  • 28
  • 188
  • 178
224
votes
7 answers

Python version <= 3.9: Calling class staticmethod within the class body?

When I attempt to use a static method from within the body of the class, and define the static method using the built-in staticmethod function as a decorator, like this: class Klass(object): @staticmethod # use as decorator def…
martineau
  • 119,623
  • 25
  • 170
  • 301
217
votes
8 answers

decorators in the python standard lib (@deprecated specifically)

I need to mark routines as deprecated, but apparently there's no standard library decorator for deprecation. I am aware of recipes for it and the warnings module, but my question is: why is there no standard library decorator for this (common) task…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
1
2 3
99 100