4

New to Python, just writing a noddy example, getting a weird error.

display.py

import abc
from graphics import *

class Display:
    pass

class Visual(metaclass=abc.ABCMeta):
    """Represents a thing which can be drawn on a display"""

    @abc.abstractmethod
    def draw(disp: Display) -> None:
        """Draws the visual to the display"""
        raise NotImplementedError()


class Display(metaclass=abc.ABCMeta):
    def __init__(self) -> None:
        __visuals = []

    def add_visual( vis: Visual ):
        __visuals.append(vis)

    def draw() -> None:
        for visual in __visuals:
            visual.draw(self)

graphics_display.py

from graphics import *
from gfx.display import Display

class GraphicsDisplay(Display):
    def __init__(self, window : GraphWin) -> None:
        super().__init__()
        __window = window

    def get_window() -> GraphWin:
        return __window

The traceback is

>>> win = GraphWin()
>>> display = GraphicsDisplay(window=win)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/julian/test/gfx_test/gfx/graphics_display.py", line 6, in __init__
    Display.__init__(self)
TypeError: __init__() takes 1 positional argument but 2 were given

graphics.py is here

Why does it think the base init is getting 2 arguments?

Julian Gold
  • 1,246
  • 2
  • 19
  • 40

2 Answers2

-1

You've defined the argument window as a positional argument but then initialized it using a keyword argument.

This is the correct way based on how you defined GraphicsDisplay init:

display = GraphicsDisplay(win)

Alternatively, change your init definition to be a keyword argument with a default value:

class GraphicsDisplay(Display):
    def __init__(self, window : GraphWin=None) -> None:
        super().__init__()
        __window = window
VoteCoffee
  • 4,692
  • 1
  • 41
  • 44
  • Unless the parameter is defined as a positional-only parameter (`def __init__(self, window, /): ...`, you can use either a positional or a keyword argument to assign the parameter a value. – chepner Feb 26 '23 at 15:12
-2

That line should look like this:

super(GraphicsDisplay, self).__init__()

When you're calling super() without arguments, you're getting non-bound parent class instance, so method __init__() don't get self objects passed as first argument.

The problem also might be with class overriding

bakatrouble
  • 1,746
  • 13
  • 19