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
Post a Comment