1

As I was reading this tutorial about tkinter for my first steps with GUI in Python, I realized that a class function was called inside the __init__ function.

I'd to like to understand, for what reason must be declared self.class_function() when calling a class function inside __init__ function?

I found this question with this answer below, which just says that it must be called self.class_function and not class_function, without providing any concept or explanation about why it must be done 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 
ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
  • 1
    You have to use self when calling class function. As it helps in identify how the function will behave for a particular object. It also helps during inheritance. Where you can change or override the base class function with another. Also the fact that on using static method. You have to provide all the instance information to the static function but what about the private class variable associated with object. How will you provide them to the static function that will be a dirty practice to follow. – Rajan Chauhan Aug 08 '17 at 04:55
  • 3
    Your example doesn't contain class methods. `parse_file` is an instance method. – Ilja Everilä Aug 08 '17 at 04:56
  • 1
    If you want, you could also call it like `MyClass.parse_file(self)` – Nick T Aug 08 '17 at 04:57
  • 2
    take a look at the [docs](https://docs.python.org/2/tutorial/classes.html#class-and-instance-variables) – Azat Ibrakov Aug 08 '17 at 04:58
  • Thanks, @AzatIbrakov. – ivanleoncz Aug 08 '17 at 05:07

2 Answers2

3

My understanding is that the reason you must call method with self.method_name instead of method_name is because, under the hood, Python substitutes the self portion to the first argument of the method.

You can test this for yourself by defining a class called Foo:

class Foo:
  def __init__(self):
    self.fun()
  def fun(self):
    print("fun")

You can actually call the fun() method by performing the following:

foo = Foo()
Foo.fun(foo)

This will be the same as performing self.fun() from within the class.

Please note that you cannot access private fields statically (e.g Foo._private_method(foo).)

duper51
  • 770
  • 1
  • 5
  • 20
  • Also the fact that info about private class variables can't be shared to static function. – Rajan Chauhan Aug 08 '17 at 05:03
  • @RajanChauhan huh? What do you mean? If you define a `_private_method` then sure you can. Python doesn't really have "private methods" anyway. – juanpa.arrivillaga Aug 08 '17 at 05:44
  • @juanpa.arrivillaga I was talking about class variable can't be shared with static function in a clean way. In order to do so you have to have their accessing functions. Yes in Python there is nothing truly private. But in true sense it will be a bad practice to follow. If you are the owner of your code. You have open boundaries no walls. – Rajan Chauhan Aug 08 '17 at 06:01
1

for what reason must be declared self.class_function() when calling a class function inside __init__ function?

self.foo() refers to instance methods of the class and not static methods or another methods from other modules...

in that way you do something similar to this.foo() in other languages, in other words: self.foo() is meaning to call a foo method of that object!

Nick T
  • 25,754
  • 12
  • 83
  • 121
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97