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 |