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 
asked Aug 8, 2017 at 4:50
5
  • 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. Commented Aug 8, 2017 at 4:55
  • 3
    Your example doesn't contain class methods. parse_file is an instance method. Commented Aug 8, 2017 at 4:56
  • 1
    If you want, you could also call it like MyClass.parse_file(self) Commented Aug 8, 2017 at 4:57
  • 2
    take a look at the docs Commented Aug 8, 2017 at 4:58
  • Thanks, @AzatIbrakov. Commented Aug 8, 2017 at 5:07

2 Answers 2

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).)

answered Aug 8, 2017 at 4:57
Sign up to request clarification or add additional context in comments.

3 Comments

Also the fact that info about private class variables can't be shared to static function.
@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 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.
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
27k14 gold badges88 silver badges128 bronze badges
answered Aug 8, 2017 at 4:53

1 Comment

"and not static methods" No - staticmethods will be called, also, so will classmethods.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.