简体   繁体   English

Python使用super覆盖属性,传递参数时出现意外参数错误

[英]Python using super to override attributes, unexpected argument error when passing arguments

I have a piece class for a chess program which I want other pieces to inherit from:我有一个国际象棋程序的棋子类,我希望其他棋子继承自:

class Piece:
    def __init__(self, player, diag, straight, opponent, marker, unicode):

        self.player = player
        self.diag = diag
        self.straight = straight
        self.opponent = opponent
        self.marker = marker
        self.unicode = unicode

and for example, a king class that must set attributes that are specific to itself but part of the base class attributes:例如,一个国王类必须设置特定于自身但属于基类属性的属性:

from Piece import Piece

class King(Piece):

    def __init__(self, player, opposition, unicode):
        super.__init__(player,False, False, opposition,"K", unicode)
        self.has_moved = False
        self.casle_king = True
        self.castle_queen = True

My problem is that when I pass these into the super function, they throw an unexpected argument error even though I have defined them in the piece.我的问题是,当我将这些传递给 super 函数时,即使我已经在片段中定义了它们,它们也会抛出意外的参数错误。

What is the correct way for the King to inherit from Piece but also set particular attributes that they share differently?国王继承 Piece 的正确方法是什么,但同时也设置了他们不同共享的特定属性?

You forgot the parentheses on super:你忘记了 super 的括号:

super().__init__(player,False, False, opposition,"K", unicode)

Or:或者:

Piece.__init__(self, player,False, False, opposition,"K", unicode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM