gecos-lab / PZero

GNU Affero General Public License v3.0
22 stars 2 forks source link

use super() in methods #69

Closed andrea-bistacchi closed 2 weeks ago

andrea-bistacchi commented 4 months ago

I've just learnt how to use super() in methods within classes in a way that allows defining more general code in the superclass, and then adding more code in the same method in the subclass, without substituting the hole method, thus avoiding repetition.

For example, I define the same method in BaseView() (superclass) and VTKView() (subclass):

class BaseView():
    -- more code here --
    def initialize_menu_tools(self):
        """This is the base method of the abstract BaseView() class, used to add menu tools used by all windows.
        The code appearing here is appended in subclasses using super().initialize_menu_tools() in their first line."""
        self.removeEntityButton = QAction("Remove Entity", self)  # create action
        self.removeEntityButton.triggered.connect(
            self.remove_entity
        )  # connect action to function
        self.menuBaseView.addAction(self.removeEntityButton)  # add action to menu
        self.toolBarBase.addAction(self.removeEntityButton)  # add action to toolbar
    -- more code here --

class VTKView():
    -- more code here --
        def initialize_menu_tools(self):
        """This is the intermediate method of the VTKView() abstract class, used to add menu tools used by all VTK windows.
        The code appearing here is appended in subclasses using super().initialize_menu_tools() in their first line."""
        # append code from BaseView()
        super().initialize_menu_tools()
        # then add new code specific to VTKView()
        self.saveHomeView = QAction("Save home view", self)  # create action
        self.saveHomeView.triggered.connect(
            self.save_home_view
        )  # connect action to function
        self.menuBaseView.addAction(self.saveHomeView)  # add action to menu
        self.toolBarBase.addAction(self.saveHomeView)  # add action to toolbar
        self.zoomHomeView = QAction("Zoom to home", self)
        self.zoomHomeView.triggered.connect(self.zoom_home_view)
        self.menuBaseView.addAction(self.zoomHomeView)
        self.toolBarBase.addAction(self.zoomHomeView)
        -- more code here --
andrea-bistacchi commented 4 months ago

I see @gbene was already using thin in some methods. I am trying to make this more systematic to avoid code duplication.

andrea-bistacchi commented 2 weeks ago

Now included as standard practice in Wiki.