Calling a class function inside __init__

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(): #do some parsing self.stat1 = result_from_parse1 self.stat2 = result_from_parse2 self.stat3 = result_from_parse3 self.stat4 = result_from_parse4 self.stat5 = result_from_parse5 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(): #do some parsing self.stat1 = result_from_parse1 self.stat2 = result_from_parse2 self.stat3 = result_from_parse3 self.stat4 = result_from_parse4 self.stat5 = result_from_parse5 

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?

+54
python class
Sep 28 '12 at 19:40
source share
6 answers

Call the function as follows:

 self.parse_file() 

You also need to define your parse_file () function as follows:

 def parse_file(self): 

The parse_file method must be bound to the object when it is called (because it is not a static method). This is done by calling the function on the object instance, in your case, the self instance.

+76
Sep 28 '12 at 19:45
source share

If I'm not mistaken, both functions are part of your class, you should use it as follows:

 class MyClass(): def __init__(self, filename): self.filename = filename self.stat1 = None self.stat2 = None self.stat3 = None self.stat4 = None self.stat5 = None self.parse_file() def parse_file(self): #do some parsing self.stat1 = result_from_parse1 self.stat2 = result_from_parse2 self.stat3 = result_from_parse3 self.stat4 = result_from_parse4 self.stat5 = result_from_parse5 

replace the line:

 parse_file() 

from:

 self.parse_file() 
+13
Sep 28 '12 at 19:45
source share

What about:

 class MyClass(object): def __init__(self, filename): self.filename = filename self.stats = parse_file(filename) def parse_file(filename): #do some parsing return results_from_parse 

By the way, if you have variables named stat1 , stat2 , etc., the situation will ask for a tuple: stats = (...) .

So let parse_file return a tuple and store the tuple in self.stats .

Then, for example, you can access what was formerly called stat3 , with self.stats[2] .

+5
Sep 28 '12 at 19:45
source share

In parse_file take the self argument (as in __init__ ). If you need a different context, just pass it as optional arguments, as usual.

0
Sep 28 '12 at 19:47
source share

You must declare parse_file like this; def parse_file(self) . The "self" parameter is a hidden parameter in most languages, but not in python. You must add it to the definition of all those methods that belong to the class. Then you can call the function from any method inside the class using self.parse_file

Your latest program will look like this:

 class MyClass(): def __init__(self, filename): self.filename = filename self.stat1 = None self.stat2 = None self.stat3 = None self.stat4 = None self.stat5 = None self.parse_file() def parse_file(self): #do some parsing self.stat1 = result_from_parse1 self.stat2 = result_from_parse2 self.stat3 = result_from_parse3 self.stat4 = result_from_parse4 self.stat5 = result_from_parse5 
0
Sep 28 '12 at 19:47
source share

I think your problem is with the indentation of the init function incorrectly. It should be like

 class MyClass(): def __init__(self, filename): pass def parse_file(): pass 
-5
Sep 28 '12 at 19:44
source share



All Articles