This respects the resolution order in case of multiple inheritance and, for Python 3.x, protects from changes in the class hierarchy. Always use super(cls, self) for Python 2.x or super() for Python 3.x to call the original implementation of a method. When in need of skipping the call be sure to document the reasons. This meakes the underlying API work as expected. Call the original implementation of a method you are overriding whenever possible. Be sure to know what you are doing, however, and document why you are completely overwriting the method. In those cases you may prefer to skip the call to the original implementation of the method Python does not make it mandatory, so feel free to walk that path if you think the situation requires it. Shall we always call the original method implementation? In theory a well designed API should make it always possible but we know that boundary cases exist: the original method may have side effect that you want to avoid and sometimes the API cannot be refactored to avoid them. There is therefore no fixed position for the call of the original method, and it depends on what you want to do. When you use the FileCatNoEmpty object you get the result of the FileCat object with the empty lines stripped.Īs you can see while in the first example the original implementation has been called as the last thing, in the second one it is called before everything else. cat ( filepath ) nonempty_lines = return nonempty_lines close () return lines class FileCatNoEmpty ( FileCat ): def cat ( self, filepath ): lines = super ( FileCatNoEmpty, self ). Import os class FileCat ( object ): def cat ( self, filepath ): f = file ( filepath ) lines = f. Since you have to explicitly call the parent implementation you are free to do it where you want in the code of the new method: the decision about the type of filtering you want to achieve affects the position of the call. Obviously both things may be done together in the same method. You typically want to filter arguments (pre-filter) if you want to change the data that the parent implementation shall process while you filter the results (post-filter) if you want to add an additional processing layer. When you override you have to think if you want to filter the arguments for the original implementation, if you want to filter its results, or both. This surely follows the Python idea that "Explicit is better than implicit" ( The Zen of Python), but this advice is not just a matter of taste or some sort of programming mannerism. Python makes you call the original implementation of an overridden method explicitly (not differently from other object-oriented languages). This means that any method call may hide a complex set of operations on the whole class hierarchy, and some of them may be vital for the library or the framework you are using. When you inherit from a class, you are actually inheriting a whole class hierarchy which internal structure is (or shall be considered) unknown. This reason may be called "hidden side effects". There is however a well defined reason why you must always call the original implementation. So, by calling the original implementation, you get the result you later want to improve. You may want to call it because many times you override a method to enhance its nature, that is to improve the "quality" of the result, and to improve something you need to first access it. Why should you want to call the implementation of objects that are deeper in the class hierarchy? So, if you want to run the implementation of one or more of the ancestors of your class, you have to call them explicitly. Inheritance delegation occours automatically, but if a method is overridden the implementation of the ancestors is not considered at all. This is of uttermost importance in Python. Since now the Child class actually contains a get_value() method with a different implementation (the id of the two functions are different). p = Parent () > c = Child () > dir ( p ) > dir ( c ) > dir ( Parent ) > dir ( Child ) > Parent.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |