Linked Questions
54 questions linked to/from What does functools.wraps do?
10 votes
1 answer
10k views
'global name @wraps is not defined' error in flask [duplicate]
I have a flask app, where i have implemented a snippet, to check if a user is logged in, in order to acces certain webpages on my application. My method looks like this: #check if session is ...
5 votes
2 answers
4k views
Function name of wrapped function? [duplicate]
How can I get the name of the original function? def wrap(f): def wrapped_f(*args, **kwargs): # do something return wrapped_f @wrap def A(params): # do something print(A.__name__)...
0 votes
0 answers
91 views
python functools wraps when to use it and not to use it [duplicate]
I'm trying wrap my head around python decorators so I borrowed this supposedly fastest memoization implementation : class memoize(dict): def __init__(self, func): self.func = func ...
0 votes
1 answer
66 views
Unexpected behaviour with decorator [duplicate]
Let's suppose I have a simple function returning its own name def foo(): print(foo.__name__) Of course the output when calling is foo. However, if we now decorate that function with a decorator ...
0 votes
0 answers
29 views
function attributes in decorated function [duplicate]
Is there a way to set all attributes in a decorated function as in the original function? For example if I take this code: def timeit(f): import time def new_f(n): start = time.time()...
194 votes
8 answers
209k views
How to decorate a class?
How do I create a decorator that applies to classes? Specifically, I want to use a decorator addID to add a member __id to a class, and change the constructor __init__ to take an id argument for that ...
147 votes
9 answers
31k views
Preserving signatures of decorated functions
Suppose I have written a decorator that does something very generic. For example, it might convert all arguments to a specific type, perform logging, implement memoization, etc. Here is an example: ...
143 votes
10 answers
49k views
How can I simulate 2.x's tuple unpacking for lambda parameters, using 3.x?
In Python 2, I can write: In [5]: points = [ (1,2), (2,3)] In [6]: min(points, key=lambda (x, y): (x*x + y*y)) Out[6]: (1, 2) But that is not supported in 3.x: File "<stdin>", line ...
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 ...
66 votes
9 answers
29k views
Python decorator makes function forget that it belongs to a class
I am trying to write a decorator to do logging: def logger(myFunc): def new(*args, **keyargs): print 'Entering %s.%s' % (myFunc.im_class.__name__, myFunc.__name__) return myFunc(*...
47 votes
3 answers
31k views
Python Class Based Decorator with parameters that can decorate a method or a function
I've seen many examples of Python decorators that are: function style decorators (wrapping a function) class style decorators (implementing __init__, __get__, and __call__) decorators which do not ...
22 votes
7 answers
20k views
Python add to a function dynamically
how do i add code to an existing function, either before or after? for example, i have a class: class A(object): def test(self): print "here" how do i edit the class wit ...
7 votes
4 answers
2k views
How to write metaclass which would prevent creating new attributes after __init__()?
Currently I override the class' __setattr__() towards the end of the class' __init__() method to prevent new attribute creation - class Point(object): def __init__(self): self.x = 0 ...
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_ = '...