Linked Questions
29 questions linked to/from Preserving signatures of decorated functions
64 votes
6 answers
57k views
Get the name of a decorated function? [duplicate]
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 ...
0 votes
1 answer
769 views
Tab completion of decorated functions in IPython Notebook [duplicate]
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 ...
0 votes
1 answer
107 views
decorator - Setting a wrapped functions argument names and values [duplicate]
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 ...
1051 votes
7 answers
340k views
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 ...
374 votes
21 answers
315k views
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 (&...
410 votes
4 answers
545k views
Getting list of parameter names inside python function [duplicate]
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'] ...
52 votes
8 answers
37k views
Set function signature in Python
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
Python decorator to keep signature and user defined attribute
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_ = '...
12 votes
3 answers
8k views
decorator module vs functools.wraps
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, ...
5 votes
2 answers
2k views
How to override default help message on python function
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 """ ...
3 votes
2 answers
1k views
Nice Python Decorators
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 ...
6 votes
3 answers
161 views
Number of actual function arguments
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 ...
2 votes
2 answers
3k views
Applying functools.wraps to nested wrappers
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 ...
2 votes
1 answer
2k views
How to dynamically change signatures of method in subclass?
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(...
4 votes
1 answer
898 views
Show the original function when logging funcName in a decorator
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(...