Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am coming from a Java background but I am fairly confident that the same principles will apply in Python. As a rule of thumb you should <b>never</b> inherit from a class whose implementation you don't understand and control unless that class has been designed specifically for inheritance. If it has been designed in this way it should describe this clearly in its documentation.</p> <p>The reason for this is that inheritance can potentially bind you to the implementation details of the class that you are inheriting from.</p> <p>To use an example from Josh Bloch's book 'Effective Java'</p> <p>If we were to extend the class <code>ArrayList</code> class in order to be able to count the number of items that were added to it during its life-time (not necessarily the number it currently contains) we may be tempted to write something like this.</p> <pre><code>public class CountingList extends ArrayList { int counter = 0; public void add(Object o) { counter++; super.add(0); } public void addAll(Collection c) { count += c.size(); super.addAll(c); } // Etc. } </code></pre> <p>Now this extension looks like it would accurately count the number of elements that were added to the list but in fact it may not. If <code>ArrayList</code> has implemented <code>addAll</code> by iterating over the <code>Collection</code> provided and calling its interface method <code>addAll</code> for each element then we will count each element added through the <code>addAll</code> method twice. Now the behaviour of our class is dependent on the implementation details of <code>ArrayList</code>.</p> <p>This is of course in addition to the disadvantage of not being able to use other implementations of <code>List</code> with our <code>CountingList</code> class. Plus the disadvantages of inheriting from a concrete class that are discussed above.</p> <p>It is my understanding that Python uses a similar (if not identical) method dispatch mechanism to Java and will therefore be subject to the same limitations. If someone could provide an example in Python I'm sure it would be even more useful.</p>
    singulars
    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. 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.
    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