python - python中的字段继承访问

标签 python oop inheritance

我对继承有疑问。

这是我的主程序:

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") [未被覆盖],后来在第二个类上调用了 getfundict() [被覆盖],但是当我尝试访问 f. size() 它总是失败并出现 TypeError: must be type, not classobj。

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

有什么帮助吗?

非常感谢。

最佳答案

这里有两个问题:

super 类和旧式类:

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

TypeError: must be type, not classobj .

继承类中不正确的super调用:

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

因此,Python 会尝试找到一个 reqboxfileparser 的类继承自它实现了你正在寻找的东西:f .

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

have a look at the documentation对于最常见的 super调用语法。

您的解决方案

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

此外,您应该始终使用 new-style classes (但这并不能解决你的问题,你会得到一个不同的错误提示 AttributeError: 'super' object has no attribute 'f' ,这将是 True ,因为 object 不提供 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调用与使用 ParentClass.stuff 非常相似, 只有它能更好地处理多重继承,因此应该使用它。

你可以在这里看到,reqboxfileparser.f会提出 AttributeError .


脚注:a classobj是一个老式的类,一个type是一个新型类。

关于python - python中的字段继承访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12734748/

相关文章:

oop - 系统设计和面向对象设计之间的区别

design-patterns - 这是已知模式吗?

C++ 继承,隐藏的基本方法

python - 从 np.argpartition 索引更快地索引 3D NumPy 数组

c++ - 访问者模式是区分 C++ 中参数类型的最快方法吗?

java - 有没有办法制作一个非抽象但必须被覆盖的方法?

C++包含和重新定义类错误

python - vagrant 和 ubunutu : pip install jupyter error: Storing debug log for failure in/home/vagrant/. pip/pip.log

python - 使用 Python 从 Azure Key Vault 解码 JsonWebKey

python - 我可以在 Golang 中加载经过 Python 训练的分类器吗?