Linked Questions

64 votes
6 answers
57k views

here is my decorator: def check_domain(func): def wrapper(domain_id, *args, **kwargs): domain = get_object_or_None(Domain, id=domain_id) if not domain: return None ...
RadiantHex's user avatar
  • 25.7k
0 votes
1 answer
769 views

In the following example code below, we have an undecorated function fun() and a decorated one wrappedfun(). For the undecorated function fun, pressing TAB in IPython notebook after the opening ...
Karthik V's user avatar
  • 1,897
0 votes
1 answer
107 views

Is it possible to give a wrapped function the arg and kwargs names of the function it is wrapping? I need to do this because decorators that are applied later use the arg names of the underlying ...
Brendan Maguire's user avatar
1051 votes
7 answers
340k views

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 ...
Eli Courtwright's user avatar
374 votes
21 answers
315k views

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 (&...
Staale's user avatar
  • 28.1k
410 votes
4 answers
545k views

Is there an easy way to be inside a python function and get a list of the parameter names? For example: def func(a,b,c): print magic_that_does_what_I_want() >>> func() ['a','b','c'] ...
R S's user avatar
  • 11.9k
52 votes
8 answers
37k views

Suppose I have a generic function f. I want to programmatically create a function f2 that behaves the same as f, but has a customized signature. More detail Given a list l and and dictionary d I ...
9 votes
4 answers
3k views

I have my simple decorator my_decorator which decorates the my_func. def my_decorator(func): def wrapper(*args, **kwargs): return func(*args, **kwargs) wrapper._decorator_name_ = '...
Jayendra Parmar's user avatar
12 votes
3 answers
8k views

The functionality of the decorator module and functools.wraps is closely related. What are the differences between the two (as of Python 3.3 / 3.4)? I am aware of one difference: 3+ years ago, ...
max's user avatar
  • 52.7k
5 votes
2 answers
2k views

I have a function with many arguments and a detailed help message, e.g.: def worker_function(arg1, arg2, arg3): """ desired help message: arg1 - blah arg2 - foo arg3 - bar """ ...
glexey's user avatar
  • 811
3 votes
2 answers
1k views

How do I nicely write a decorator? In particular issues include: compatibility with other decorators, preserving of signatures, etc. I would like to avoid dependency on the decorator module if ...
Casebash's user avatar
  • 120k
6 votes
3 answers
161 views

Is there a way to get the number of actual parameters passed to a function def foo(a, optional=42): if ???? print "1 arg" else: print "2 args" foo(11) # should print 1 ...
georg's user avatar
  • 216k
2 votes
2 answers
3k views

I have a base decorator that takes arguments but that also is built upon by other decorators. I can't seem to figure where to put the functools.wraps in order to preserve the full signature of the ...
Tom's user avatar
  • 1,326
2 votes
1 answer
2k views

When using classmethod to dynamic change the method in subclass, how to dynamic change signatures of method? example import inspect class ModelBase(object): @classmethod def method_one(...
Ryan's user avatar
  • 235
4 votes
1 answer
898 views

I have a decorator to retry some operations for 3 times if a deadlock happens, and does some logging in the process: def retry_on_deadlock(func): logger = logging.getLogger('test') @wraps(...
GergelyPolonkai's user avatar

15 30 50 per page