Python 新手,刚刚写了一个简单的例子,遇到了一个奇怪的错误。
显示.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)
Run Code Online (Sandbox Code Playgroud)
图形显示.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
Run Code Online (Sandbox Code Playgroud)
回溯是
>>> 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
Run Code Online (Sandbox Code Playgroud)
Graphics.py 在这里
为什么它认为基础init获得了 2 个参数?
bak*_*ble -2
该行应该如下所示:
super(GraphicsDisplay, self).__init__()
Run Code Online (Sandbox Code Playgroud)
当您不带参数调用时super(),您将获得非绑定父类实例,因此方法__init__()不会获取self作为第一个参数传递的对象。
问题也可能与类覆盖有关
| 归档时间: |
|
| 查看次数: |
9598 次 |
| 最近记录: |