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.
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. )
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
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)
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!
~~ 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)
--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.
Recommended Posts