实现类时出现Python错误

2024-09-27 04:24:00 发布

您现在位置:Python中文网/ 问答频道 /正文

class DetailedScore(Score):
'''A subclass of Score adding level'''

    def __init__(self, points, initials, level):
        '''
        (Score, int, str, int) -> NoneType

        Create a score including number of points, initials, and level.
        '''

        super().__init__(points, initials)
        self.level = level

    def __str__(self):
        '''
        Return a string representation of DetailedScore formated:

        'The student with initials 'KTH' scored 100 points, the student is in level 10'
        '''

        score_str = super().__str__()

        return '{}, the student is in level {}'.format(score_str, self.level)

    def __repr__(self):
        '''
        Return a string representation of DetailedScore formated:

        'DetailedScore(100, 'KTH', 10)'
        '''

        return 'DetailedScore({}, {}, {})'.format(self.points, self.initials, self.level)

score5 = DetailedScore(1000, 'JQP', 100)
score6 = DetailedScore(999, 'ABC', 99)
score7 = DetailedScore(999, 'BBB', 15)
score8 = DetailedScore(1, 'KTH', 12)

我正在努力完成这门课,不知道为什么我总是得到一个错误时,试图建立

这是错误:

Traceback (most recent call last):
  File "/Users/KoryHershock/Documents/Python/[Kory_Hershock]_final.py", line 187, in <module>
    score5 = DetailedScore(1000, 'JQP', 100)
  File "/Users/KoryHershock/Documents/Python/[Kory_Hershock]_final.py", line 162, in __init__
    super().__init__(points, initials)
TypeError: super() takes at least 1 argument (0 given)
[Finished in 0.1s with exit code 1]

Tags: ofinselfinitdeflevelstudentpoints
2条回答

super().__whatever__()更改为super(Score, self).__whatever__()

如果您使用的是python2,那么必须编写super(DetailedScore, self),而不是super()

python3也允许无参数形式,编译器插入从词法上下文获取的适当类对象

相关问题 更多 >

    热门问题