Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>I don't see why you should generally avoid to derive from built-in types. It was considered an improvement that the arrival of new-style classes enabled to do this, and even before this the standard library provided the <code>UserDict</code> and <code>UserList</code> classes to derive from. Of course you have to evaluate in every specific case whether inheritance or composition is what you need, but that's how in always is in OOP -- nothing special about built-in types here.</p></li> <li><p>I wouldn't recommend <code>super()</code> in the example. It is only useful if all cooperating methods have the same signature, or at least cooperating signatures. For single inheritance and "diamond-free" multiple inheritance, explicitly calling base-class methods is fine in my opinion. If you want to get things right in multiple inheritance with diamonds, you are forced to use <code>super()</code>, but you have to design your methods carefully, see <a href="http://rhettinger.wordpress.com/2011/05/26/super-considered-super/" rel="nofollow">Raymond Hettinger's "Python's <code>super()</code> considered super!"</a></p> <p>It is generally safe to use <code>super()</code> in cases where all involved methods have exactly the same signature, including methods likes <code>__getattr__()</code> and <code>__setattr__()</code>.</p></li> <li><p>Your example class is a rather questionable implementation of an <code>Enum</code> type, but not for any of the reasons you mentioned in your post. It mixes up two namespaces: The namespace of the attributes of <code>dict</code> and the enumeration entries. Try <code>Enum("clear", "update")</code> to see what I mean. In this particular case, the follwing implementation would be much better:</p> <pre><code>class Enum(object): def __init__(self, *values): vars(self).update({mnemo: num for num, mnemo in enumerate(values)}) </code></pre> <p>This adds the enumeration entries as attributes to a standard instance without deriving from <code>dict</code>. Every custom class instance brings its own dictionary anyway, namely <code>__dict__</code>.</p></li> </ol>
 

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