Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A function does not "have" a class -- one or more classes <em>may</em> (but need not) refer to a given function object, e.g.:</p> <pre><code>def f(self): return g() class One(object): bah = f class Two(object): blup = f def g(): ... </code></pre> <p><code>One.bah</code> and <code>Two.blup</code> are both set as references to the same function object <code>f</code> (and turn into unbound or bound method objects when accessed on the classes or their instances, but that leaves no trace in the stack). So, the stack upon</p> <pre><code>One().f() </code></pre> <p>vs</p> <pre><code>Two().f() </code></pre> <p>as seen from the <code>g()</code> that's called in either case, is pretty hard indeed to distinguish -- as would be that resulting from, e.g.,</p> <pre><code>f('blah bloh blup') </code></pre> <p>with no "classes" involved at all (though the <em>type</em> of the string being used as <code>f</code>'s <code>self</code> argument would be;-).</p> <p>I recommend <strong>not</strong> relying on introspection -- especially the wild and wooly kind, with lots of heuristics, that would be needed to get any semi-useful info here -- for any <em>production</em> code requirements: rearchitect to avoid that need.</p> <p>For purely <em>debugging</em> purposes, in this case, you could walk the stack until you find a calling function with a first argument named <code>self</code> and introspect the type of the value bound to that argument name -- but as I mentioned that's entirely heuristical, since nothing forces your callers to name their first argument <code>self</code> if and only if their functions are meant to be methods in some class.</p> <p>Such heuristic introspection would produce type objects <code>One</code>, <code>Two</code>, and <code>str</code>, in the three examples of code I just gave -- as you see, definitely not good for much else than debugging;-).</p> <p>If you clarify better exactly <em>what</em> you're trying to accomplish through this attempted introspection, we might of course be able to help you better.</p>
    singulars
    1. This table or related slice is empty.
    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. 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