Python 从'matplotlib.patches.RegularPolygon'继承会在实例化时产生'ValueError'

Python 从'matplotlib.patches.RegularPolygon'继承会在实例化时产生'ValueError',python,matplotlib,Python,Matplotlib,我正在尝试从matplotlib.patches.RegularPolygon派生一个类。 当前的目标是为matplotlib.patches.Circle和matplotlib.patches.RegularPolygon提供某种程度上统一的API,它们在某些属性上有所不同,然后我可以在应用程序的其余部分中使用它们。由于到目前为止所有代码都是为圆编写的,因此修改RegularPolygon的属性是有意义的 然而,由于某些原因,参数传递不正确-至少这是我迄今为止最好的解释。我可以用一些指针来说明

我正在尝试从matplotlib.patches.RegularPolygon派生一个类。 当前的目标是为matplotlib.patches.Circle和matplotlib.patches.RegularPolygon提供某种程度上统一的API,它们在某些属性上有所不同,然后我可以在应用程序的其余部分中使用它们。由于到目前为止所有代码都是为圆编写的,因此修改RegularPolygon的属性是有意义的

然而,由于某些原因,参数传递不正确-至少这是我迄今为止最好的解释。我可以用一些指针来说明为什么会这样。我已经盯着源代码看了很长一段时间,没有看到它,因为我找错了地方

MWE 错误消息 使用

您正在调用matplotlib.patches.RegularPolygon父级的init函数。但是,您确实需要调用matplotlib.patches.RegularPolygon本身的init

我还建议不要为子类艺术家使用相同的名称,因为这可能会在这里增加混淆

你有什么选择

旧式

import matplotlib

class MyRegularPolygon(matplotlib.patches.RegularPolygon):
    def __init__(self, *args, **kwargs):
        matplotlib.patches.RegularPolygon.__init__(self, *args, **kwargs)

r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)
新型py2和PY3

import matplotlib

class MyRegularPolygon(matplotlib.patches.RegularPolygon):
    def __init__(self, *args, **kwargs):
        super(MyRegularPolygon, self).__init__(*args, **kwargs)

r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)
仅新样式py3

import matplotlib

class MyRegularPolygon(matplotlib.patches.RegularPolygon):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)

我建议大家阅读一些有关super的解释。

谢谢欧内斯特。正如我所怀疑的那样,我变得很麻木。A正常的一天,我知道超级工作原理,我保证!*在正常的一天。这个不是。显然。不要忘记调试的许多可能步骤之一就是呼吸新鲜空气或明天再看一次-
import matplotlib

class MyRegularPolygon(matplotlib.patches.RegularPolygon):
    def __init__(self, *args, **kwargs):
        matplotlib.patches.RegularPolygon.__init__(self, *args, **kwargs)

r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)
import matplotlib

class MyRegularPolygon(matplotlib.patches.RegularPolygon):
    def __init__(self, *args, **kwargs):
        super(MyRegularPolygon, self).__init__(*args, **kwargs)

r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)
import matplotlib

class MyRegularPolygon(matplotlib.patches.RegularPolygon):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

r = MyRegularPolygon((0.,0.), 5, radius=5, orientation=0)