Popular recipes tagged "inspection" but not "debug"http://code.activestate.com/recipes/tags/inspection-debug/2012-10-26T12:59:47-07:00ActiveState Code RecipesFind what class an attribute - ie, myObj.myAttr - comes from, and how it's defined (Python)
2012-10-26T12:59:47-07:00Paul Molodowitchhttp://code.activestate.com/recipes/users/4184064/http://code.activestate.com/recipes/578305-find-what-class-an-attribute-ie-myobjmyattr-comes-/
<p style="color: grey">
Python
recipe 578305
by <a href="/recipes/users/4184064/">Paul Molodowitch</a>
(<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/__dict__/">__dict__</a>, <a href="/recipes/tags/__getattribute__/">__getattribute__</a>, <a href="/recipes/tags/__getattr__/">__getattr__</a>, <a href="/recipes/tags/__slots__/">__slots__</a>).
Revision 3.
</p>
<p>When inspecting new code (or when debugging), it can be handy to know exactly where a given attribute on an object or class comes from.</p>
<p>As a simple example, if you have a class MyClass, you might want to know where MyClass().myMethod is defined.</p>
<p>However, things can get tricky when things like __getattr__, __getattribute__, or even compiled objects come into play. That's where this function comes in. It returns what class a given attribute comes from, and what method was used to define it - ie, '__dict__' ('normal' definitions), '__slots__', '__getattr__', '__getattribute__', '(BUILTIN)'.</p>
<p>(Note - this function should't be relied on to be 100% accurate - rather, it's a best guess, for where to look to find it. It takes some pretty infrequent edge cases for it to be wrong, though...)</p>
Inspect a PYC File (Python)
2011-09-29T20:07:10-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577880-inspect-a-pyc-file/
<p style="color: grey">
Python
recipe 577880
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/bytecode/">bytecode</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/pyc/">pyc</a>).
Revision 3.
</p>
<p>This is a revamp of a recipe that Ned Batchelder <a href="http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html">posted on his blog</a> a few years ago (thanks Ned!). That page is pretty insightful, recipe aside.</p>
<p>This recipe works for all versions of Python back to 2.4 (at least). Warning: using one version of Python to inspect a pyc file from another Python version may not work too well.</p>
print variables as a dictionarly (Python)
2009-03-27T07:39:13-07:00Maxim Khesinhttp://code.activestate.com/recipes/users/2591465/http://code.activestate.com/recipes/576700-print-variables-as-a-dictionarly/
<p style="color: grey">
Python
recipe 576700
by <a href="/recipes/users/2591465/">Maxim Khesin</a>
(<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/print/">print</a>).
Revision 2.
</p>
<p>Often I need to print some diagnostic variables and have to construct a format string to include both name and value; but there is a quicker way! This is especially useful for tracing function call values (foo in example below)</p>