Questions tagged [python-decorators]

In Python, decorators are functions that conveniently alter functions, methods or classes using a special syntax. Decorators dynamically alter the functionality without changing the source code being decorated.

In Python, decorators are functions that conveniently alter functions, methods or classes using a special syntax:

@decorator_function
def python_function():
    '''a python function'''

Which is equivalent to this:

python_function = decorator_function(python_function)

python.org wiki

1888 questions
3949
votes
32 answers

Difference between staticmethod and classmethod

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?
Daryl Spitzer
  • 121,723
  • 75
  • 151
  • 166
2901
votes
18 answers

How to make function decorators and chain them together?

How can I make two decorators in Python that would do the following? @makebold @makeitalic def say(): return "Hello" ...which should return: "Hello" I'm not trying to make HTML this way in a real application - just trying to…
Imran
  • 76,055
  • 23
  • 93
  • 124
1100
votes
14 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
  • 20,778
  • 27
  • 68
  • 89
706
votes
14 answers

What does the "at" (@) symbol do in Python?

I'm looking at some Python code which used the @ symbol, but I have no idea what it does. I also do not know what to search for as searching Python docs or Google does not return relevant results when the @ symbol is included.
AJ00200
  • 12,717
  • 7
  • 20
  • 20
193
votes
7 answers

Class method decorator with self arguments?

How do I pass a class field to a decorator on a class method as an argument? What I want to do is something like: class Client(object): def __init__(self, url): self.url = url @check_authorization("some_attr", self.url) def…
Mark
  • 3,496
  • 5
  • 22
  • 32
171
votes
11 answers

How do I avoid the "self.x = x; self.y = y; self.z = z" pattern in __init__?

I see patterns like def __init__(self, x, y, z): ... self.x = x self.y = y self.z = z ... quite frequently, often with a lot more parameters. Is there a good way to avoid this type of tedious repetitiveness? Should the class…
146
votes
10 answers

Real world example about how to use property feature in python?

I am interested in how to use @property in Python. I've read the python docs and the example there, in my opinion, is just a toy code: class C(object): def __init__(self): self._x = None @property def x(self): """I'm the…
xiao 啸
  • 5,732
  • 8
  • 37
  • 49
108
votes
1 answer

Decorator execution order

def make_bold(fn): return lambda : "" + fn() + "" def make_italic(fn): return lambda : "" + fn() + "" @make_bold @make_italic def hello(): return "hello world" helloHTML = hello() Output: "hello world" I…
Newbie
  • 1,197
  • 2
  • 8
  • 13
106
votes
5 answers

How do I pass extra arguments to a Python decorator?

I have a decorator like below. def myDecorator(test_func): return callSomeWrapper(test_func) def callSomeWrapper(test_func): return test_func @myDecorator def someFunc(): print 'hello' I want to enhance this decorator to accept another…
balki
  • 22,482
  • 26
  • 85
  • 135
70
votes
5 answers

How to bypass python function definition with decorator?

I would like to know if its possible to control Python function definition based on global settings (e.g. OS). Example: @linux def my_callback(*args, **kwargs): print("Doing something @ Linux") return @windows def my_callback(*args,…
Pedro
  • 1,009
  • 6
  • 14
66
votes
3 answers

Decorating Python class methods - how do I pass the instance to the decorator?

This is Python 2.5, and it's GAE too, not that it matters. I have the following code. I'm decorating the foo() method in bar, using the dec_check class as a decorator. class dec_check(object): def __init__(self, f): self.func = f def…
Phil
  • 3,038
  • 3
  • 28
  • 39
58
votes
11 answers

How to inject variable into scope with a decorator?

[Disclaimer: there may be more pythonic ways of doing what I want to do, but I want to know how python's scoping works here] I'm trying to find a way to make a decorator that does something like injecting a name into the scope of another function…
beardc
  • 16,995
  • 16
  • 66
  • 88
54
votes
3 answers

Python functools lru_cache with class methods: release object

How can I use functools' lru_cache inside classes without leaking memory? In the following minimal example the foo instance won't be released although going out of scope and having no referrer (other than the lru_cache). from functools import…
televator
  • 713
  • 1
  • 6
  • 7
52
votes
4 answers

Is a Python Decorator the same as Java annotation, or Java with Aspects?

Are Python Decorators the same or similar, or fundamentally different to Java annotations or something like Spring AOP, or Aspect J?
bn.
  • 7,031
  • 7
  • 37
  • 53
48
votes
6 answers

how to do a conditional decorator in python

Is it possible to decorator a function conditionally. For example, I want to decorate the function foo() with a timer function (timeit) only doing_performance_analysis is True (see the psuedo-code below). if doing_performance_analysis: @timeit …
cfpete
  • 3,373
  • 8
  • 22
  • 23
1
2 3
99 100