Alb*_*upp 2 python oop inheritance
我有继承问题.
这是我的主要计划:
def main(argv):
rfp = reqboxfileparserng() # Inherits from reqboxfileparser()
rfp.importsdir = './data/'
if rfp.parsingasutf8_win():
rfp.parsefile("./data/LRCv12.txt")
Run Code Online (Sandbox Code Playgroud)
以下是课程:
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 ...
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有两个类,第一个是reqboxfileparser(),第二个是reqboxfileparserng(),它继承自第一个类.
在主程序上我调用了方法:parsefile("./ data/LRCv12.txt")[未覆盖],后来在第二个类上调用getfundict()[overrided],但是当我尝试访问f.size()时它始终因TypeError而失败:必须是type,而不是classobj.
已经有一段时间了,因为我没有开发具有继承的类,但如果我没有错,那么这些概念是正确的.我是Python的新手.
有什么帮助吗?
非常感谢.
这里有两个问题:
class reqboxfileparser():object因此,不会继承,super(reqboxfileparser, self)总会产生错误:
TypeError: must be type, not classobj.
你正在做super(reqboxfileparser, self),但你将继承的class(reqboxfileparser)作为第一个参数传递,而不是继承类.
因此,Python会尝试找到一个reqboxfileparser继承自实现您正在寻找的内容的类:f.
但是,这不是要你想:你想要什么的祖先reqboxfileparserng实现f; 那会是reqboxfileparser.
你可能已经猜到了你应该使用它super(reqboxfileparserng, self).
另外,你应该总是使用新式的类(但仅凭这一点不能解决你的问题,你会得到一个不同的错误抱怨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()
Run Code Online (Sandbox Code Playgroud)
总的来说,一个super调用非常类似于使用ParentClass.stuff,只是它更好地处理多重继承,因此应该使用它.
你可以看到,在这里,reqboxfileparser.f会提出一个AttributeError.
脚注:a classobj是一个旧式的类,一个type是新式的类.