継承に問題があります。
これは私のメインプログラムです:
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 ...
ご覧のとおり、2 つのクラスがあります。1 つ目は reqboxfileparser() で、2 つ目は最初のクラスを継承する reqboxfileparserng() です。
メインプログラムでメソッドを呼び出します: parsefile("./data/LRCv12.txt") [オーバーライドされていません] 後で 2 番目のクラスで getfundict() [オーバーライドされました] が呼び出されますが、f.size() にアクセスしようとするとTypeError: must be type, not classobj で常に失敗します。
継承を使用してクラスを開発しないようになってからしばらく経ちましたが、私が間違っていなければ、概念は正しいです。私はPythonの初心者です。
何か助けてください。
どうもありがとう。