python - When should behaviour that is to be customized in subclasses be put in seperate methods? -


consider (python):

assume global functions: default_start(), main_behaviour(), default_end(), custom_start(), , custom_end() code filler illustration purposes.

class parent:     def on_start_main(self):         default_start()      def main_behaviour(self):         main_behaviour()      def on_end_main(self):         default_end()      def main(self):         self.on_start_main()         self.main_behaviour()         self.on_end_main()  class child(parent):     def on_start_main(self):         custom_start()      def on_end_main(self):         custom_end() 

vs

class parent:     def main_behaviour(self):         main_behaviour()      def main(self):         default_start()         self.main_behaviour()         default_end()  class child(parent):     def main(self):         custom_start()         parent.main_behaviour(self)         custom_end() 

i don't know of these preferable, suspect second. matter of taste or there concrete reasons why 1 better other?

thanks

i prefer first solution 1 simple reason:

what if want in child class override on_start_main , leave on_end_main unchanged.

if choose first solution, override on_start_main method , done. don't have know or care parent class in it's on_end_main.

if choose second solution have know parent class in main method, not have dig source of parent class duplicate code written.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -