我遇到了繼承問題。Python中的字段繼承權限
這是我的主要程序:
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()[overRided],但是當我嘗試訪問f .size()它總是以TypeError失敗:必須是type,而不是classobj。
這是一段時間,因爲我不開發繼承類,但如果我沒有錯,概念是正確的。我是Python的新手。
請幫忙嗎?
非常感謝。
值得注意的是,Python約定是將類名稱放在CapWords中 - 檢查[python style guide](http://www.python.org/dev/peps/pep-0008/)。 –
您可以發佈錯誤消息,而不是描述它嗎? – lolopop