__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python

Fixed phrases often seen in Tkinter, wxPython constructors

When creating a class that inherits any widget such as tk.Frame, wx.Panel in Tkinter, wxPython, there is a fixed phrase that is always in the sample and is also used by myself.

class MyTkinterFrame(tk.Frame):
    def __init__(self, master = None):
        #this!
        tk.Frame.__init__(self, master)
        ...

class MyWxPanel(wx.Panel):
    def __init__(self, parent = None):
        #This too!
        wx.Frame.__init__(self, master = None):
        ...

I don't understand the meaning of this process, but if I don't add it, an error will occur, so I thought it was a spell for the time being, but when I think about it carefully, I'm just calling the constructor of the inheriting class **. I noticed it, so make a note.

Isn't it too obvious for those who know it? Also, even if you don't know it, you can spend a wonderful Tkinter, wxPython Life if you write it for the time being. Please use it as trivia.

Supplement @ 2017-01-18

I received a comment, but I made a mistake. In the text, __init___ is called the constructor, and the call to `__ init__``` is called the constructor call. But Python's ``` __init __``` is not a constructor. I'm sorry that I understand that it's an initialization process when I say something, but ... Reference: [About the base class ``` __init__ ()`](http://kk6.hateblo.jp/entry/20110429/1304074964) I didn't know the proper replacement, so I didn't correct the whole sentence. Please read carefully about the part called the constructor in the text. )

Implicit argument self

In Python, the implicit argument `` `self``` is hidden when calling a class instance method. The next call can call the same process.

class SomeClass:
    def SomeMethod(self):
        # do something
        ...
    
    def OtherMethod(self):
        self.SomeMethod()   #Part 1

    def AnotherMethod(self):
        # SomeMethod(self)    #Part 2<-Postscript:This call will result in an error, but I wrote it due to lack of confirmation, so I fixed it.
        SomeClass.SomeMethod(self)   #Part 2

By the way, it could be called both in * 2 * notation and outside the class.

instance = SomeClass()
instance.SomeMethod()     #Part 1
SomeClass.SomeMethod(instance)  #Part 2

Call `` `init``` of the inheriting class from the inheriting class

In Python, the constructor of the inheriting class does not implicitly call the inherited class. Therefore, when inheriting, it is necessary to explicitly call the constructor of the inheritance source.

class MyInherit(MySuper):
    def __init__(self):
       #Inheritor constructor call
       #The following notation can only be used with Python3
       super().__init__()

Or

class MyInherit(MySuper):
    def __init__(self):
       #Inheritor constructor call,Python 2 is OK
       super(MySuper, self).__init__()

It's a very personal matter, but I rarely program with inheritance. However, it seems to be quite normal in Python, Tkinter, and wxPython, so I learned it.

(Reference: How to use Python's super () function)

Align the explicit self specification with the init call of the inheriting class

And the fixed phrase in question.

Calls to the constructor `__ init__``` of the inheriting class can be made without using super () . This may be more common as a __ init__``` call to the inheriting class. You are calling by explicitly passing `` self``` as an argument.

class MyInherit(MySuper):
    def __init__(self):
       #Inheritor constructor call
       # Python 2,Possible in both Python 3
       MySuper.__init__(self)
       ...

I see!

Why didn't you notice this?

~~ Pride and belief ~~ The language I used most so far was C #, and the method of calling the inheritor constructor was different. Also, when it comes to class methods, I thought it was natural to call `` `. (...) ```.

The C # inheritance source constructor is to be called when the constructor is declared.

class MyInherit : MySuper
{
    //constructor
    public MyInherit(int val) : MySuper(val)
    {
        // do initialize...
    }
}

I haven't touched C ++ or Java, but it seems that an implicit inheritor constructor call is made as well.

(Reference: C # constructor and destructor)

Summary

--In Python, an explicit call to the constructor __init__ of the inheriting class is ** required **. --The fixed phrase for inheritance is by explicitly specifying the argument of self. ――You are a little familiar with the differences between programming languages.

For me personally, I would like to remember how to use and write, and to explain why there is such a difference as ** cultural difference ** and move on to the next step.

Also, I think that `super () .__ init__ ()` is excellent in terms of readability and ease of communicating meaning, but if it is a function from Python3, it will be penetrated from now on. It will be a story. I want to use it positively.

Confirmed environment

Recommended Posts

__init__ called by wxPython or Tkinter was a __init__ call of the inheriting class in Python
How to use the __call__ method in a Python class
Read the standard output of a subprocess line by line in Python
Get the caller of a function in Python
Make a copy of the list in Python
Output in the form of a python array
You can call the method of the class directly in Python3 without adding @staticmethod
Group by consecutive elements of a list in Python
A reminder about the implementation of recommendations in Python
Find out the apparent width of a string in python
I made a class to get the analysis result by MeCab in ndarray with python
[Python] Which is executed first, the class variable or __init__?
[Note] Import of a file in the parent directory in Python
[Python3] Call by dynamically specifying the keyword argument of the function
Find the eigenvalues of a real symmetric matrix in Python
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
Create an instance of a predefined class from a string in Python
A simple data analysis of Bitcoin provided by CoinMetrics in Python
[Understanding in the figure] Management of Python virtual environment by Pipenv
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
A function that measures the processing time of a method in python
Get the number of readers of a treatise on Mendeley in Python
Examine the object's class in python
Extension of Python by C or C ++ (when there are multiple arguments, when passing a list from the Python side)
Get a capture of the entire web page in Selenium Python VBA
If you want a singleton in python, think of the module as a singleton
A memo organized by renaming the file names in the folder with python
Check the in-memory bytes of a floating point number float in Python
Is the space replaced by a plus sign or% 20 in percent-encoding processing?
Test & Debug Tips: Create a file of the specified size in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
Hit a method of a class instance with the Python Bottle Web API
Receive a list of the results of parallel processing in Python with starmap
How to format a list of dictionaries (or instances) well in Python
Get a datetime instance at any time of the day in Python
I made a program to check the size of a file in Python
How to sort by specifying a column in the Python Numpy array.
Generate a first class collection in Python
Check the behavior of destructor in Python
[Learning memo] Basics of class by python
Write the test in a python docstring
Display a list of alphabets in Python 3
OR the List in Python (zip function)
Connect a lot of Python or and and
Run the Python interpreter in a script
The result of installing python in Anaconda
Read the file line by line in Python
Read the file line by line in Python
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
Generate a class from a string in Python
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python
The story of blackjack A processing (python)
A story made by a person who has no knowledge of Python or Json
Various ways to read the last line of a csv file in Python
How to pass the execution result of a shell command in a list in Python
Python scikit-learn A collection of predictive model tips often used in the field