Really sorry for the extremely stupid title, but if I know what it is, I wouldn’t write here (:
def some_decorator( func ):
# ..
class A:
@some_decorator
def func():
pass
@func.some_decorator # this one here - func.some_decorator ?
def func():
pass
some_decorator decorates func – that’s OK. But what is func.some_decorator and how some_decorator becomes a member ( or something else ? ) of func?
P.S. I’m 90% sure, that there’s such question here (as this seems something basic), but I don’t know how to search it. If there’s a exact duplicate, I’ll delete this question.
Note : It’s not typo, nor accident, that both member functions are named func. The decorator is for overloading: the question is related to Decorating method (class methods overloading)
Remember that the function definition with decorator is equivalent to this:
So in the following lines,
funcdoesn’t refer to the function you defined but to what the decorator turned it into. Also note that decorators can return any object, not just functions. Sosome_decoratorreturns an object with a method (it’s unfortunate that the namessome_decoratorandfuncare reused in the example – it’s confusing, but doesn’t change anything about the concept) that is itself a decorator. As the expression after the@is evaluated first, you still have a reference to the first decorator’s method after you defined another plain functionfunc. That decorator is applied to this new function. The full example is then equivalent to this: