I write code that takes a file name, opens a file and parses some data. I would like to do it in class. The following code works:
class MyClass(): def __init__(self, filename): self.filename = filename self.stat1 = None self.stat2 = None self.stat3 = None self.stat4 = None self.stat5 = None def parse_file():
But this is because I put all the parsing mechanisms in the scope of the __init__ function for my class. Now this looks great for this simplified code, but the parse_file function also has several levels of indentation. I would prefer to define the parse_file() function as a class function, as shown below:
class MyClass(): def __init__(self, filename): self.filename = filename self.stat1 = None self.stat2 = None self.stat3 = None self.stat4 = None self.stat5 = None parse_file() def parse_file():
Of course, this code does not work, because the parse_file() function is not in the scope of the __init__ function. Is there a way to call a class function from __init__ this class? Or am I thinking about it wrong?
python class
PythonJin Sep 28 '12 at 19:40 2012-09-28 19:40
source share