I should begin by saying this is being run in IPython, using the Spyder IDE if it makes a difference. I am somewhat of a newbie at Python but fluent in other languages.
I have a class which is instantiated and used in a function, like below. I have simplified the code from a very long reading and processing algorithm. This all works fine the first time I run this file and call ReadFile(). Any subsequents runs crash, basically because m_seclist and m_headers already contain data.
My understanding of classes comes from C++, PHP and VB so I may be off with my assumptions, but as the class is instantiated within a function (ie local scope), should the instance not be completely destroyed at the end of the function? In this case it appears it very definitely isn't, or at least some of the variables are surviving. Am I misunderstanding how Python deals with class instances?
class Reader:
m_headers = []
m_seclist = []
def __init__(self, filename):
fh = open(filename, 'r')
file_contents = fh.read().splitlines()
fh.close()
dataPointList = []
for line in file_contents:
for each section in the file (more code here)
thisLine = line.split()
dataPointList.append((float(thisLine[1]), float(thisLine[0])))
m_seclist.append(dataPointList)
dataPointList = []
def getData(self, index):
return m_seclist[index][0]
#end of class
def ReadFile(filename):
my_instance = Reader(filename)
output = my_instance.getData(2)
return output
If I can clarify something, yell.