Note that there are some explanatory texts on larger screens.

plurals
  1. POPython instancemethods & decorators
    primarykey
    data
    text
    <p>I've got the following problem:</p> <p>I need to write a decorator that would be able to detect a situation where it is used around a method and around a regular function.</p> <p>So let's consider the following code:</p> <pre><code>def somedecorator(fn): print "decorating function:", fn print "function class:", fn.__class__ return fn @somedecorator def regular_func(): print "I'm a regular function" class SomeClass(object): @somedecorator def class_method(self): print "I'm a class method" </code></pre> <p>The output reads:</p> <pre><code>decorating function: &lt;function regular_func at 0x181d398&gt; function class: &lt;type 'function'&gt; decorating function: &lt;function class_method at 0x181d410&gt; function class: &lt;type 'function'&gt; </code></pre> <p>However writing:</p> <pre><code>print regular_func print SomeClass.class_method print SomeClass().class_method </code></pre> <p>Produces output:</p> <pre><code>&lt;function regular_func at 0x181d398&gt; &lt;unbound method SomeClass.class_method&gt; &lt;bound method SomeClass.class_method of &lt;__main__.SomeClass object at 0x16d9e50&gt;&gt; </code></pre> <p>So as it turns out (not so surprisingly) that decorators are applied to functions before they are turned into methods. </p> <p>Question: What would you suggest to distinguish the two applications from the decorator point of view?</p> <p><strong>EDIT</strong> For the curious ones - I need the described distinction because of pickling. I don't really want to go into details on this one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload