【问题标题】:Python - call super() as attribute of subclassPython - 调用 super() 作为子类的属性
【发布时间】:2018-07-25 00:35:49
【问题描述】:
class Point(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

class Triangle(Point):

    def __init__(self, v1, v2, v3):
        """Create a new Triangle with vertices (v1, v2, v3)."""
        self.v1 = super().__init__(x=v1[0], y=v1[1])
        self.v2 = super().__init__(x=v2[0], y=v2[1])
        self.v3 = super().__init__(x=v3[0], y=v3[1])

tri = Triangle((1,1),(1,10),(1,5))

我正在尝试调用 super().__init__() 作为子类的属性,但是当我执行上面的代码时,我收到一条错误消息:

__init__() missing 1 required positional argument: 'y'

它指向子类中定义 self.v1 的行。这里有什么明显的错误吗?我尝试将 self 和其他测试参数添加到 super().__init__() 调用中只是为了测试一下,但我总是得到同样的错误。

【问题讨论】:

  • 您的代码在 Python 3.6 中对我来说没有错误。你确定你在运行 python 3?
  • 这看起来像是 python3 代码的 python2 错误。
  • @FHTMitchell 是的,我在Python 3 中没有收到错误,但在Python 2 中出现错误。
  • 如果我错了,请纠正我,但我认为你应该使用super().__new__(x=v1[0], y=v1[1])。通过使用__init__,您只是替换了Triangle 实例中的x 和y 属性。
  • 我在进行编辑,然后在同一个 ipython 会话中测试代码。我不得不重新启动我的 ipython 会话,然后它就起作用了。总是简单的...

标签: python python-3.x superclass


【解决方案1】:

您的代码存在一些问题。

super().__init__ 没有按照你的想法做。在每次调用__init__ 时,您实际上是在tri 范围内运行Point.__init__。如果您print(tri),您将看到tri 同时具有x 和y 属性。

tri = Triangle((1,1),(1,10),(1,5))
print(tri.x)  # 1
print(tri.y)  # 5

你真正想做的是为每个顶点创建一个Point 的实例。为此并使用super,您可以使用self.v1 = super().__new__(x=v1[0], y=v1[1])。 __new__ 将构造super 类的新对象并将该对象分配给self.v1。

class Triangle(Point):
    def __init__(self, v1, v2, v3):
        """Create a new Triangle with vertices (v1, v2, v3)."""
        self.v1 = super().__new__(x=v1[0], y=v1[1])
tri = Triangle((1,1),(1,10),(1,5))
print(tri.v1)  # Point (x=1, y=1
print(tri.v2)  # Point (x=1, y=10)

现在您有了一个可行的示例,但您仍然可以做得更好。看到您在Triangle 中使用的Point 的唯一部分是__new__,我们为什么还要从该类继承?在这种情况下,我将删除继承并只创建新实例而不调用super()。

另外,在 python 3 中,您不需要从 object 继承。

class Point():

    def __init__(self, x, y):
        self.x = x
        self.y = y

class Triangle():

    def __init__(self, v1, v2, v3):
        """Create a new Triangle with vertices (v1, v2, v3)."""
        self.v1 = Point(x=v1[0], y=v1[1])
        self.v2 = Point(x=v2[0], y=v2[1])
        self.v3 = Point(x=v3[0], y=v3[1])

tri = Triangle((1,1),(1,10),(1,5))
print(tri.v1)  # Point (x=1, y=1
print(tri.v2)  # Point (x=1, y=10)

作为额外的 - 我会推荐 @Oliver 的实现。它更干净,不会创建那么多对象。唯一的副作用是将 v1 重命名为 x 等。

【讨论】:

  • 就是这样,谢谢。此外,我意识到我试图创建的“三角形”实际上是一条线。将顶点更改为 (1,1)、(10,1) 和 (1,5) 更适合我想要完成的任务。
  • Marshall,请看@Oliver 的回答,因为我觉得这会是一个更好的实现方式。
【解决方案2】:

您的问题实际上与super 无关,而与整个继承有关。

如果A 的对象实际上定义为具有额外属性的B 的对象,则A 类应该继承自B 类。继承扩展了定义。

在Triangle 和Point 的情况下,您真正​​想要的代码(已在 Alexander Kamyanskiy 和 Jim Wright 的答案中给出)不需要继承。此外,从数学上讲,三角形不是具有额外属性的点。

这是一个有意义的继承示例。

class Point:
    def __init__(self, x, y)
        self.x = x
        self.y = y

# A point in 3D really is a point in 2D with an extra coordinate
class Point3D(Point)
    def __init__(self, x, y, z):
        self.z = z
        super().__init__(x, y)

这是一个继承的例子,虽然在编程上是正确的,但实际上并没有什么意义。

class Plant:
    def __init__(self, type):
        self.type= type

# Let me check my biology textbooks...
class Animal(Plant):
    def __init__(self, kind, favorite_food):
        self.kind = kind
        # Herbivores only please
        self.favorite_food = Plant(favorite_food)

使用另一个类作为属性不会强制你继承它。在上面的例子中,你告诉 Python 任何 Animal 也是 Plant 的一个实例,根据我的小生物学背景是不正确的。

【讨论】:

  • +1 实现简单!我最初使用继承,因为它是 OP 所采用的方法,所以我专注于修复它。
【解决方案3】:

当你被召唤时

self.v1 = super().__init__(x=v1[0], y=v1[1])

它调用 Point 类的 __init__() 方法,但不返回 Point 实例,它返回 None(并将 x,y 属性添加到您的 tri Triangle 类实例)。

用 3 Points 代码实例化三角形可能如下所示:

class Point(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

class Triangle(object):

    def __init__(self, v1, v2, v3):
        """Create a new Triangle with vertices (v1, v2, v3)."""
        self.v1 = Point(x=v1[0], y=v1[1])
        self.v2 = Point(x=v2[0], y=v2[1])
        self.v3 = Point(x=v3[0], y=v3[1])

tri = Triangle((1,1),(1,10),(1,5))

     17         self.v1 = Point(x=v1[0], y=v1[1])
---> 18         self.v2 = Point(x=v2[0], y=v2[1])
     19         self.v3 = Point(x=v3[0], y=v3[1])

ipdb> self.v1
<__main__.Point object at 0x7f18c48da048>
ipdb> self.v1.x
1

【讨论】:

  • 我要补充一点,Point by Triangle 的继承在这里毫无意义。
  • 同意你的观点,@Olivier
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 2021-04-25
  • 1970-01-01
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
相关资源
最近更新 更多