Linked Questions

10 votes
1 answer
10k views

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 ...
kristof's user avatar
  • 141
5 votes
2 answers
4k views

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__)...
varantir's user avatar
  • 6,904
0 votes
0 answers
91 views

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 ...
d-coder's user avatar
  • 14.2k
0 votes
1 answer
66 views

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 ...
Raumschifffan's user avatar
0 votes
0 answers
29 views

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()...
othymomo's user avatar
  • 105
194 votes
8 answers
209k views

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 ...
Robert Gowland's user avatar
147 votes
9 answers
31k views

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: ...
Fredrik Johansson's user avatar
143 votes
10 answers
49k views

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 ...
balki's user avatar
  • 27.9k
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
66 votes
9 answers
29k views

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(*...
Charles Anderson's user avatar
47 votes
3 answers
31k views

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 ...
Adam Parkin's user avatar
22 votes
7 answers
20k views

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 ...
Timmy's user avatar
  • 12.9k
7 votes
4 answers
2k views

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 ...
work.bin's user avatar
  • 1,108
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

15 30 50 per page