<p>If I have Python code</p>

<pre><code>class A():
    pass
class B():
    pass
class C(A, B):
    pass
</code></pre>

<p>and I have class <code>C</code>, is there a way to iterate through it's super classed (<code>A</code> and <code>B</code>)? Something like pseudocode:</p>

<pre><code>&gt;&gt;&gt; magicGetSuperClasses(C)
(&lt;type 'A'&gt;, &lt;type 'B'&gt;)
</code></pre>

<p>One solution seems to be <a href="http://docs.python.org/lib/module-inspect.html" rel="noreferrer">inspect module</a> and <code>getclasstree</code> function.</p>

<pre><code>def magicGetSuperClasses(cls):
    return [o[0] for o in inspect.getclasstree([cls]) if type(o[0]) == type]
</code></pre>

<p>but is this a "Pythonian" way to achieve the goal?</p>

[{'answer_id': 25815, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': 6, 'selected': True, 'text': '<p><code>C.__bases__</code> is an array of the super classes, so you could implement your hypothetical function like so:</p>\n\n<pre><code>def magicGetSuperClasses(cls):\n  return cls.__bases__\n</code></pre>\n\n<p>But I imagine it would be easier to just reference <code>cls.__bases__</code> directly in most cases.</p>\n'}, {'answer_id': 35111, 'author': 'cdleary', 'author_id': 3594, 'author_profile': 'https://Stackoverflow.com/users/3594', 'pm_score': 4, 'selected': False, 'text': '<p>@John: Your snippet doesn\'t work -- you are returning the <em>class</em> of the base classes (which are also known as metaclasses). You really just want <code>cls.__bases__</code>:</p>\n\n<pre><code>class A: pass\nclass B: pass\nclass C(A, B): pass\n\nc = C() # Instance\n\nassert C.__bases__ == (A, B) # Works\nassert c.__class__.__bases__ == (A, B) # Works\n\ndef magicGetSuperClasses(clz):\n  return tuple([base.__class__ for base in clz.__bases__])\n\nassert magicGetSuperClasses(C) == (A, B) # Fails\n</code></pre>\n\n<p>Also, if you\'re using Python 2.4+ you can use <a href="http://www.python.org/dev/peps/pep-0289/" rel="noreferrer">generator expressions</a> instead of creating a list (via []), then turning it into a tuple (via <code>tuple</code>). For example:</p>\n\n<pre><code>def get_base_metaclasses(cls):\n    """Returns the metaclass of all the base classes of cls."""\n    return tuple(base.__class__ for base in clz.__bases__)\n</code></pre>\n\n<p>That\'s a somewhat confusing example, but genexps are generally easy and cool. :)</p>\n'}, {'answer_id': 552862, 'author': 'Torsten Marek', 'author_id': 9567, 'author_profile': 'https://Stackoverflow.com/users/9567', 'pm_score': 2, 'selected': False, 'text': '<p>The inspect module was a good start, use the <a href="http://docs.python.org/library/inspect.html#inspect.getmro" rel="nofollow noreferrer">getmro</a> function:</p>\n\n<blockquote>\n  <p>Return a tuple of class cls’s base classes, including cls, in method resolution order. No class appears more than once in this tuple. ...</p>\n</blockquote>\n\n<pre><code>&gt;&gt;&gt; class A: pass\n&gt;&gt;&gt; class B: pass\n&gt;&gt;&gt; class C(A, B): pass\n&gt;&gt;&gt; import inspect\n&gt;&gt;&gt; inspect.getmro(C)[1:]\n(&lt;class __main__.A at 0x8c59f2c&gt;, &lt;class __main__.B at 0x8c59f5c&gt;)\n</code></pre>\n\n<p>The first element of the returned tuple is <code>C</code>, you can just disregard it.</p>\n'}, {'answer_id': 553927, 'author': 'sinzi', 'author_id': 62904, 'author_profile': 'https://Stackoverflow.com/users/62904', 'pm_score': 2, 'selected': False, 'text': "<p>if you need to know to order in which super() would call the classes you can use <code>C.__mro__</code> and don't need <code>inspect</code> therefore.</p>\n"}]
['https://Stackoverflow.com/questions/25807', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2679/']