Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Note:</strong> in the following I assume Python 3.x. For Python 2.x, use <a href="http://www.python.org/doc/newstyle/" rel="nofollow noreferrer">new style classes</a>, i.e. write <code>class T(object): pass</code> instead of <code>class T: pass</code>.</p> <ol> <li><p>First, define some classes:</p> <pre><code>&gt;&gt;&gt; class T: pass ... &gt;&gt;&gt; class S: pass ... &gt;&gt;&gt; class U(T): pass ... </code></pre></li> <li><p>To get a list of classes defined in the current scope, iterate over the <a href="http://docs.python.org/3.1/library/functions.html#globals" rel="nofollow noreferrer"><code>globals</code></a> (or <a href="http://docs.python.org/3.1/library/functions.html#locals" rel="nofollow noreferrer"><code>locals</code></a>) and test whether they are an instance of <a href="http://docs.python.org/3.1/library/functions.html#type" rel="nofollow noreferrer"><code>type</code></a> (thus a class!) using <a href="http://docs.python.org/3.1/library/functions.html#isinstance" rel="nofollow noreferrer"><code>isinstance</code></a>:</p> <pre><code>&gt;&gt;&gt; [n for n, o in globals().items() if isinstance(o, type)] ['S', 'U', 'T'] </code></pre></li> <li><p>Use <a href="http://docs.python.org/3.1/library/functions.html#issubclass" rel="nofollow noreferrer"><code>issubclass</code></a> to restrict your search to the subclasses of a given class (in this case <code>T</code>):</p> <pre><code>&gt;&gt;&gt; [n for n, o in globals().items() if isinstance(o, type) and issubclass(o, (T,))] ['U', 'T'] </code></pre></li> <li><p>You may want to omit <code>T</code> itself:</p> <pre><code>&gt;&gt;&gt; [n for n, o in globals().items() if o != T and isinstance(o, type) and issubclass(o, (T,))] ['U'] </code></pre></li> <li><p>To get all classes starting with a certain string, call <a href="http://docs.python.org/3.1/library/stdtypes.html#str.startswith" rel="nofollow noreferrer"><code>startswith</code></a> on the class name:</p> <pre><code>&gt;&gt;&gt; [n for n, o in globals().items() if n.startswith('T') and isinstance(o, type)] ['T'] </code></pre></li> <li><p>To <em>flag</em> certain classes at creation, use a <a href="http://www.python.org/dev/peps/pep-3129/" rel="nofollow noreferrer">class decorator</a><sup>&dagger;</sup> to add an attribute, e.g. <code>__flagged__</code>:</p> <pre><code>&gt;&gt;&gt; def flag(cls): ... cls.__flag__ = 'flagged' ... return cls ... &gt;&gt;&gt; @flag ... class X: pass ... &gt;&gt;&gt; @flag ... class Y: pass ... &gt;&gt;&gt; class Z: pass ... </code></pre></li> <li><p>Now you can just select only those classed with the <code>__flag__</code> attribute:</p> <pre><code>&gt;&gt;&gt; [n for n, o in globals().items() if isinstance(o, type) and hasattr(o, '__flag__')] ['X', 'Y'] </code></pre></li> </ol> <hr> <p>&nbsp;&nbsp;<sup>&dagger;</sup>: As <a href="https://stackoverflow.com/users/18936/bobince">bobince</a> notes, class decorators are <a href="http://docs.python.org/dev/3.0/whatsnew/2.6.html#pep-3129-class-decorators" rel="nofollow noreferrer">new in Python 2.6</a>.</p>
 

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