python-decorators

Possible to create a @synchronized decorator that's aware of a method's object?

一世执手 提交于 2020-11-26 19:34:38
问题 I'm trying to create a @synchronized wrapper that creates one Lock per object and makes method calls thread safe. I can only do this if I can access method.im_self of the method in the wrapped method. class B: def f(self): pass assert inspect.ismethod( B.f ) # OK assert inspect.ismethod( B().f ) # OK print B.f # <unbound method B.f> print B().f # <bound method B.f of <__main__.B instance at 0x7fa2055e67e8>> def synchronized(func): # func is not bound or unbound! print func # <function f at

Possible to create a @synchronized decorator that's aware of a method's object?

吃可爱长大的小学妹 提交于 2020-11-26 19:34:27
问题 I'm trying to create a @synchronized wrapper that creates one Lock per object and makes method calls thread safe. I can only do this if I can access method.im_self of the method in the wrapped method. class B: def f(self): pass assert inspect.ismethod( B.f ) # OK assert inspect.ismethod( B().f ) # OK print B.f # <unbound method B.f> print B().f # <bound method B.f of <__main__.B instance at 0x7fa2055e67e8>> def synchronized(func): # func is not bound or unbound! print func # <function f at

How to document Python functions with overload/dispatch decorators?

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-26 06:54:51
问题 Applying Documentation to Multi-Dispatched Functions I am using the multipledispatch package, in a fashion similar to the example code below. I want to be able to see the docstring text when I ask for help(my_add) in the Python command line, but instead all I see is information about the decorator. Functools.wraps must be the way to do it, but how? I have looked up functools.wraps, which I'm sure is what I want to use. I have found examples of how to use it, such as this and this. But I still

Class decorator for methods from other class [duplicate]

最后都变了- 提交于 2020-08-25 09:18:32
问题 This question already has answers here : Decorating Python class methods - how do I pass the instance to the decorator? (3 answers) Closed 11 days ago . NOTE: I've got a related question here: How to access variables from a Class Decorator from within the method it's applied on? I'm planning to write a fairly complicated decorator. Therefore, the decorator itself should be a class of its own. I know this is possible in Python (Python 3.8): import functools class MyDecoratorClass: def __init__