【发布时间】:2019-09-18 23:04:18
【问题描述】:
我有这个代码:
from abc import ABCMeta, abstractmethod
class Instruction (object):
__metaclass__ = ABCMeta
def __init__(self, identifier_byte):
#type: (int) ->
self.identifier_byte = identifier_byte
@abstractmethod
def process (self):
print ("Identifier byte: ()".format(self.identifier_byte))
class LDAInstruction (Instruction):
def process (self):
super(Instruction,self).process()
with 适用于 Python 3.2 但不适用于 2.6。然后基于这个话题:TypeError: super() takes at least 1 argument (0 given) error is specific to any python version?
我把最后一行改成:
super(Instruction,self).process()
这会导致在此精确行上显示此错误消息:
AttributeError: 'super' 对象没有属性 'process'
对我来说,超级调用似乎有一个“过程”方法。 Python是否说“超级”是一个独立的对象,与指令无关?如果是,我怎么告诉它 super 只能调用基类构造函数?
如果没有,我将如何进行?感谢您的任何想法。
【问题讨论】:
-
class语句对于Instruction(或其继承树根部的任何类)是什么样的?它是继承自object的新型类吗?如果不是,super将不适用于该层次结构中的任何类。修复非常简单,只需将其更改为新样式类,它就可以工作。 2001 年 Python 2.2 引入了新样式类,绝对没有理由在 2019 年使用旧样式类。(我还建议 Python 2.6 不受支持且已过时,但这与您的问题没有直接关系。) -
@khelwood 与我所做的有什么不同?
-
@Blckknght 我编辑了我的问题并将相关课程的完整源代码放在那里。无论如何,据我所知,我正在使用新样式类。
标签: python-3.x constructor super python-2.6