3

我有继承的问题。

这是我的主要程序:

def main(argv):
  rfp = reqboxfileparserng() # Inherits from reqboxfileparser()
  rfp.importsdir = './data/'
  if rfp.parsingasutf8_win():
    rfp.parsefile("./data/LRCv12.txt")

以下是课程:

class reqboxfileparser():
  def __init__(self):
    ... removed code ...
    # Init mmap
    self.file = None
    self.f = None

  def parsefile(self, filename):
    # Public
    self.filename = filename

    # Init mmap
    self.file = codecs.open(filename, encoding='utf-8', mode='r') # open(filename, 'r')
    self.f = mmap.mmap(self.file.fileno(), 0, access=mmap.ACCESS_READ)
    self.f.seek(0) # rewind

    # Parsing stuff
    self.getfunlist()
    self.vlog(VERB_MED, "len(fun) = %d" % (len(self.funlist)))
    self.getfundict()
    self.vlog(VERB_MED, "fundict = %s" % (self.fundict))
... rest of reqboxfileparser() class code removed ...

class reqboxfileparserng(reqboxfileparser, object):
  def __init__(self):
    # Public
    reqboxfileparser.__init__(self)
    self.fundict = {}
    self.importsdir = ''

  def getfunlist(self):
    """
    Overrided method to load from a CSV file
    """
    self.funlist = []

    fh = open(self.importsdir + 'in-uc-objects.csv', 'rb')
    f = csv.reader(fh, delimiter=',')
  ... rest of the code removed, it works fine ...

  def getfundict(self):
    """
    Fills the fundict property with a dict where each element is indexed
    by the fun name and each value is an object from the model
    """
    self.__fundict = {}

    beginloc = self.bodystartloc()

    # PROBLEM!

    finalloc = super(reqboxfileparser, self).f.size() - 1
    ... rest of the code removed ...

如您所见,我有两个类,第一个是 reqboxfileparser(),第二个是继承自第一个的 reqboxfileparserng()。

在主程序上我调用方法: parsefile("./data/LRCv12.txt") [not overrided] 后来在第二个类上调用 getfunddict() [overrided],但是当我尝试访问 f.size()它总是以 TypeError 失败:必须是类型,而不是 classobj。

自从我不开发具有继承的类以来已经有一段时间了,但如果我没记错的话,这些概念是正确的。我是 Python 的新手。

请问有什么帮助吗?

非常感谢。

4

2 回答 2

6

这里有两个问题:

超级类和老式类:

class reqboxfileparser():不继承自object,因此,super(reqboxfileparser, self)总是会产生错误:

TypeError: must be type, not classobj.

继承类中不正确的超级调用:

您正在做super(reqboxfileparser, self),但您将继承的类 ( reqboxfileparser) 作为第一个参数传递,而不是继承类。

因此,Python 会尝试找到一个reqboxfileparser继承自该类的类,该类实现了您正在寻找的内容:f.

但这不是你想要的:你想要的是reqboxfileparserng那个实现的祖先f;那将是reqboxfileparser

查看文档以了解最常见的super调用语法。

您的解决方案

您现在可能已经猜到您应该使用它super(reqboxfileparserng, self)

另外,您应该始终使用新式类(但仅此一项并不能解决您的问题,您会收到一个不同的错误,抱怨AttributeError: 'super' object has no attribute 'f',这将是True, asobject不提供f)。

最后一件事...

但是在这里,您还有最后一期!

您试图引用f哪个是子类实例的属性。当您使用调用时,此属性不存在,super因为它不存在于父类的类定义中,而父类定义是super调用将使用的。(在__init__方法中)

我不会更详细地说明为什么这对 super 很重要,但这个想法基本上super只用于在类级别定义的东西。通常,方法是,因此它们是super调用的绝佳候选者。

这是一个描述我的意思的例子:

class reqboxfileparser():
    g = 'THIS IS G'

    def __init__(self):
        self.f = 'THIS IS F'
        self.g = 'THIS IS NEW G'

    def get_g(self):
        return self.g

class reqboxfileparserng(reqboxfileparser, object):
    def __init__(self):
        reqboxfileparser.__init__(self)


    def getfundict(self):
        print super(reqboxfileparserng, self).g # Prints "THIS IS G"
        print super(reqboxfileparserng, self).get_g() # Prints "THIS IS NEW G"
        print super(reqboxfileparserng, self).f # This raises an AttributeError

if __name__ == '__main__':
    o = reqboxfileparserng()
    o.getfundict()

总的来说,super调用与 using 非常相似ParentClass.stuff,只是它更好地处理了多重继承,因此应该使用它。

您可以在这里看到,reqboxfileparser.f会引发AttributeError.


脚注:aclassobj是旧式类,atype是新式类。

于 2012-10-04T20:07:32.917 回答
3

您缺少的一点是当前f的一个属性。它是继承的:当您调用 时,代码设置为. 因此,要从子类访问它,您只需执行,就像任何其他属性一样。super(...).__init__()fselfself.f

任何属性或方法都是如此。您只需要super在您的子类实际覆盖某些内容时使用,并且您需要调用超类的版本。因此,无需调用 super 即可访问parsefile,例如:self.parsefile()就可以了。

于 2012-10-04T21:15:21.790 回答