<div dir="ltr"><span style="font-family: courier new,monospace;">Hi, is there any possible way to get the class or class name inside a method decorator? For example in the code sample below:</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def decorate(func):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print type(func)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return func</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class myclass:</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> @decorate</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def foo(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pass</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">The output of this program will be the type of the supplied func in decorate, i.e. method foo. However, the type is function foo, a free function, not an instance method myclass.foo.</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">On a related note, as the actual instance method of myclass is not foo but decorate(foo), why are they called method decorators? This does not decorate methods, they decorate functions and eventually the decorated functions become methods. The name method decorator sounds a bit misleading to me.</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">So returning to my original question is there any way I can get the class inside decorate()? I guess there is not, but </span><span style="font-family: courier new,monospace;">just </span><span style="font-family: courier new,monospace;">asking to make sure.</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Speaking of decorators I'd also like to ask on the pending class decorators that should be coming in a following version of the language. Are they going to be in 2.6 or just 3.0? In the following example:</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def class_decorate(cls):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print 'class_decorate'</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return cls</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">def decorate(func):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print 'decorate'</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return func</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">@class_decorate</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class myclass:</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> @decorate</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def foo(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pass</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">what will be the correct output?</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"><br>class_decorate</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">decorate</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">or </span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">decorate</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">class_decorate</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">In essence what is the order of application of class decorators compared to the function decorators of their methods? I couldn't find any mention of that issue in any of the PEPs. I guess it would be the latter, following the behavior of metaclasses, but better be certain than speculate :)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Cheers,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">Themis</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"></div>