0

I have a method (update_database) within a MainApp class that is called when the app is closed using the event listener 'atexit'. This method is only called by this atexit listener.

How would you categorise the visibility of this method?

import atexit
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from order_management.constants import HELP_TEXT
from order_management.presentation.order_management.order_management_view import OrderManagementScreen
from order_management.presentation.components.dialog import Dialog
from order_management.data.query import Query


class MainApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._screen = Builder.load_file(
            "order_management/presentation/main.kv")
        self.__query = Query()
        self.__dialog = Dialog()

    def __build(self):
        self.theme_cls.primary_palette = "Green"
        sm = ScreenManager()
        # Instantiates OrderManagementScreen.py
        sm.add_widget(OrderManagementScreen(name='order_management'))
        return self._screen

    def handle_menu_click(self):
        self.__dialog.render_dialog("Usage Guide:", HELP_TEXT, None, None)

    def __update_database(self):
        self.__query.update_database()


if __name__ == "__main__":
    MainApp().run()
    main_app = MainApp()
    atexit.register(main_app.__update_database)  # Calls update_database on exit.

Charlie Clarke
  • 177
  • 1
  • 9
  • 1
    Does this even work? I would expect name mangling to screw it up when you try and reference `main_app.__update_database`. – khelwood Mar 26 '21 at 14:39
  • In python there is no true distinction between public and private, hence the absence of those keywords. However any method that starts with a double underscore is considered private, by python convention. – hasdrubal Mar 26 '21 at 14:43
  • @khelwood ah since I have added double underscore to denote private methods things haven't been working the same - this wasn't the case when I used a single underscore. Why is that? – Charlie Clarke Mar 26 '21 at 15:14
  • [Python name mangling](https://stackoverflow.com/q/7456807/3890632). Don't use double underscores unnecessarily. – khelwood Mar 26 '21 at 15:23

1 Answers1

0

Python does not have a mechanism to enforce access specifier rules such as Java or C++. This convention of identifying __function() as protected is just a guideline, it won't stop other programs from using those functions or variables.

Agni
  • 231
  • 2
  • 6
  • Yes I understand that it is simply convention, but if it were enforced, what would it be categorised as? – Charlie Clarke Mar 26 '21 at 15:15
  • Well, the usual convention is 'no underscore is public', single '_' is protected, and double '__' is private. So your function __update_database() would be private by convention. – Agni Mar 26 '21 at 15:38