簡體   English   中英

AttributeError:“超級”對象沒有屬性“ compare_files”

[英]AttributeError: 'super' object has no attribute 'compare_files'

具有預期效果的繼承的示例代碼。

class Animal:
    def action(self, value_1, p=None):
        print ('Parent action %d %d' % (value_1, p))
        return value_1 + 1 + p

class Dog(Animal):
    def action(self, value):
        print ('Dog action in child')
        return super(Dog, self).action(value, 1)

print(Dog().action(10))

以下是繼承的另一個示例,其中引發以下錯誤。

AttributeError:“超級”對象沒有屬性“ compare_files”

class FileCompare:
    def compare_files(self, actual_filename, ref_filename, field_to_skip=None):
        return True

class MetaFileCompare(FileCompare):
    def compare_files(self, actual_file, ref_file):
        return super(FileCompare, self).compare_files(actual_file, ref_file,
                    0)

class WorkerFileCompare(FileCompare):
    def compare_files(self, actual_file, ref_file):
        return super(FileCompare, self).compare_files(actual_file, ref_file,
                    5)

print (WorkerFileCompare().compare_files('a', 'b'))

除了類名,方法名和返回類型外,代碼幾乎相同。 聽起來好像很傻。 沒有錯字錯誤。 您能提供一些建議嗎?

更改為

class MetaFileCompare(FileCompare):
    def compare_files(self, actual_file, ref_file):
        return super(FileCompare, self).compare_files(actual_file, ref_file,
                    0)

class MetaFileCompare(FileCompare):
    def compare_files(self, actual_file, ref_file):
        return super(MetaFileCompare, self).compare_files(actual_file, ref_file,
                    0)

這是我犯的愚蠢錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM