Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, this is not possible as you have requested, because there is no inherent difference between bound methods and functions. A method is simply a function wrapped up to get the calling instance as the first argument (using Python <a href="http://users.rcn.com/python/download/Descriptor.htm" rel="nofollow noreferrer">descriptors</a>).</p> <p>A call like:</p> <pre><code>Test.test_call </code></pre> <p>which returns an unbound method, translates to</p> <pre><code>Test.__dict__[ 'test_call' ].__get__( None, spam ) </code></pre> <p>which is an unbound method, even though</p> <pre><code>Test.__dict__[ 'test_call' ] </code></pre> <p>is a function. This is because functions are descriptors whose <code>__get__</code> methods return methods; when Python sees one of these in the lookup chain it calls the <code>__get__</code> method instead of continuing up the chain.</p> <p>In effect, the 'bound-methodiness' of a function is determined at runtime, not at define-time!</p> <p>The decorator simply sees the function as it is defined, without looking it up in a <code>__dict__</code>, so cannot tell whether it is looking at a bound method.</p> <hr> <p>It <em>might</em> be possible to do this with a class decorator that modifies <code>__getattribute__</code>, but that's a particularly nasty hack. Why must you have this functionality? Surely, since you have to place the decorator on the function yourself, you could pass it an argument that says whether said function is defined within a class?</p> <pre><code>class Test: @decorate( method = True ) def test_call: ... @decorate( method = False ) def test_call: ... </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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