我对继承有疑问。
这是我的主程序:
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/