Is it possible in Python 2.6-2.7 to use the same decorator for the following task:
class ComplextCallableObject(object):
@wrap_me
def __call__(self, a1, a2):
pass
@wrap_me
def simple_function(a1, a2):
pass
Both ComplextCallableObject.__call__ and simple_function has the same args, but __call__ also has self for the first arg. In the wrap_me decorator I need an access for the function being wrapped args.
Unfortunately, at the time of definition (the class block in this case), the code cannot tell how a function will be used except by naming convention. Modifying your example a bit:
In this case,
simple_functionis implementing a function taking a target and two parameters, an instance method taking two parameters, a class method taking two parameters, and a static method taking a target and two parameters. But these uses are not bound until after the function is defined. Bothstaticmethodandclassmethodreturn a different object type, so you can tell those apart, if need be.If you did want to use convention, you could inspect the function’s first argument name to see if it is
self: