-1

I've wrote this code but I don't understand a part of it; the line that uses super(check_box, self).__init__(**kwargs) and when I remove check_box, self from it, the code still functions properly

from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.checkbox import CheckBox
from kivy.uix.gridlayout import GridLayout
class check_box(GridLayout):
    # for class name don't use keywords like CheckBox or if you use them you should use underscore in the name as well
    def __init__(self, **kwargs):
        # **kwargs imports keywords related to gridlayout class
        super().__init__(**kwargs)
        self.cols = 2
        # number of columns
        self.add_widget(Label(text="Male"))
        self.active = CheckBox(active=True)
        # making an object
        self.add_widget(self.active)
        # adding the object to the GUI

        self.add_widget(Label(text="Female"))
        self.active = CheckBox(active=True)
        self.add_widget(self.active)

        self.add_widget(Label(text="Other"))
        self.active = CheckBox(active=True)
        self.add_widget(self.active)

class CheckBoxApp(App):
    def build(self):
        return check_box()
if __name__ == "__main__":
    CheckBoxApp().run()
MR.PRDX
  • 1
  • 5
  • 3
    The arguments to `super()` were needed in earlier Python versions, they're optional now. – Barmar Aug 01 '22 at 17:13
  • Does this answer your question? "[Python super() inheritance and needed arguments](/q/15896265/90527)", "[What is the difference between super() with arguments and without arguments?](/q/57945407/90527)", "[correct way to use super (argument passing)](/q/8972866/90527)", … – outis Aug 01 '22 at 17:16
  • so it has just been simplifed with newer versions – MR.PRDX Aug 01 '22 at 17:20
  • the second link was the answer to my question – MR.PRDX Aug 01 '22 at 17:23

1 Answers1

0

check_box class inherits the GridLayout class. Calling super() inside check_box class allows us to make a proxy object (temporary object of the superclass) that allows us to access methods of the superclass class.

super().init(param1, param2, ...) simply means that we are calling the constructor method of that temporary superclass object to construct that object by feeding in parameter values.

Arguments inside the super are not used now, but they anyway work without error because they were used in old python versions.

  • 1
    `super` still accepts arguments; it's just that the arguments you use 99.99% of the time can be inferred when `super` is called inside a class definition. Python 3 added that inference. – chepner Aug 01 '22 at 17:40
  • As an example, if you have a `@staticmethod` that doesn't have any class information, the `super` parameters would be necessary. – tdelaney Aug 01 '22 at 17:52