1

I have this Button class derived from pygame.Rect:

class Button(pg.Rect):

    def __init__(self, left, top, width, height, char):
        super().__init__(left, top, width, height)
        self.char = char

    def draw_rect(window):
        pg.draw.rect(window, pg.Color("red"), "rectobject")

pg.draw.rect takes Rect object as a third argument. So I'm wondering if super() method instantiates the object of the parentclass and if so how to reference it (as we use self for class). I know this is a trivial problem but I'm interested in how these things work in Python. Thanks in advance!

kaktus_car
  • 986
  • 2
  • 11
  • 19
  • 1
    There is only one object here, which is an instance of both the parent and child classes. The `super().__init__()` call does the initialization (NOT instantiation) needed for the object to be a functional `Rect`, the rest of your `__init__()` does the initialization needed for the object to be a functional `Button`. – jasonharper Feb 04 '20 at 15:10
  • @jasonharper that explains it, thank you very much. – kaktus_car Feb 04 '20 at 15:15

2 Answers2

2

You inherited pg.rect, and you initialized your object like a pg.rect object. So your Button object have all attributes which are in the pg.rect objects. You can use a Button object in case where you can use pg.rect object. (Also you need to add self argument to draw_rect method)

import pygame as pg

display = pg.display.set_mode((100,100))

class Button(pg.Rect):

    def __init__(self, left, top, width, height, char):
        super().__init__(left, top, width, height)
        self.char = char

    def draw_rect(self, window):
        pg.draw.rect(window, pg.Color("red"), self)


b = Button(10,10,10,10,"a")

b.draw_rect(display)

pg.display.update()

And calling super().__init__ not instantiates anything, it initialize your self parameter. So your self object will have attributes like bottomleft which are coming from super().__init__.

Ekrem Dinçel
  • 1,053
  • 6
  • 17
  • Wasn't sure if usage 'self' is proper because of 'char'. Now it's clear, and yes I missed the self argument. Thanks a lot – kaktus_car Feb 04 '20 at 15:22
  • No problem, pg.draw.rect doesn't care about ``char`` attributes. – Ekrem Dinçel Feb 04 '20 at 15:24
  • 1
    and sorry, but one more question. So this is general rule that redundant arguments are just ignored in cases like this? – kaktus_car Feb 04 '20 at 15:28
  • 1
    Imagine that you wrote a class. This class have a method. For example, if you are trying to access ``self.char`` in this method, for sure you must have assigned a value to it. So yes, there is not a reason for not ignoring custom attributes. Because it don't need to access that attribute, nor change it. – Ekrem Dinçel Feb 04 '20 at 15:39
  • as a beginer I must say that this kind of explanations are very helpful. Seems I got all the answers I wanted. Thank you once again. – kaktus_car Feb 04 '20 at 15:56
2

A call to super() in a child class implicitly invokes the parent's constructor that causes the child object to have all the properties that a parent has and you can simply access them by using child.parentVar

There is a difference of syntax between python 2.x and 3.x

The syntax for invoking the parent's super and its semantics are discussed here: https://stackoverflow.com/a/33191175/12507046

Saad Naeem
  • 21
  • 4