Effective Python Learning Memorandum Day 9 [9/100]

Introduction

The other day I learned about 100 Days Of Code, which was popular on Twitter for a while. The purpose of this article is to keep a record and output how much I, as a beginner, can grow through 100 days of study. I think there are many mistakes and difficult to read. I would appreciate it if you could point out!

Teaching materials to be learned this time

Today's progress

--Progress: Pages 69-73 --Chapter 3: Classes and Inheritance ――I will write down what I often forget or didn't know about what I learned today.

Initialize the parent class with super

How to initialize a parent class from a child class

--Initialization using the __init__ method

Initialization using the __init__ method

#Parent class
class MyBaseClass(object):
   def __init__(self, value):
       self.value = value

#Child class
class MyChildClass(MyBaseClass):
    def __init__(self):
        MyBaseClass.__init__(self, 5)    #Of the parent class__init__Call and initialize the method

Problems with initialization using the __init__ method

This method works fine in simple hierarchies, but multiple inheritance can cause strange behavior if you call the superclass's __init__ method directly under the influence. Especially when inheriting diamonds, it causes unexpected behavior. Diamond inheritance means that a subclass inherits from two separate classes, and the two have the same superclass in the inheritance refurbishment. For example, define two child classes that inherit from MyBaseClass and a child class that inherits them as follows:

#Parent class
class MyBaseClass(object):
   def __init__(self, value):
       self.value = value

#Child class 1 that inherits the parent class
class TimesFive(MyBaseClass):
    def __init__(self, value):
        MyBaseClass.__init__(self, value)
        self.value *= 5

#Child class 2 that inherits the parent class
class PlusTwo(MyBaseClass):
    def __init__(self, value):
        MyBaseClass.__init__(self, value)
        self.value += 2

#Define a child class that inherits the two classes and make MyBaseClass the apex of the diamond
class ThisWay(TimesFive, PlusTwo):
    def __init__(self, value):
        TimesFive.__init__(self, value)
        PlusTwo.__init__(self, value)

foo = ThisWay(5)
print('Should be ( 5 * 5) + 2 = 27 but is', foo.value)

Output result

Should be ( 5 * 5) + 2 = 27 but is 7

The output should be 27, but this Way's argument 5 should be multiplied by 5 with TimesFive, plus 2 with PlusTwo, and now it's 7. This is because the call to PlusTwo.init resets to 5 when MyBaseClass.init is called a second time. In Python3 you can solve this problem by using super. Also, ** Python 3 should always use super. ** **

Initialization using super

class Explicit(MyBaseClass):
    def __init__(self, value):
        super(__class__, self).__init__(value * 2)

class Implicit(MyBaseClass):
    def __init__(self, value):
        super().__init__(value * 2)

print('Explicit', Explicit(10).value)
print('Implicit', Implicit(10).value)

Output result

Explicit 20
Implicit 20

Summary

--Python standard method resolution order solves superclass initialization order and diamond inheritance problems --Always use the built-in function super to initialize the parent class

Recommended Posts

Effective Python Learning Memorandum Day 15 [15/100]
Effective Python Learning Memorandum Day 6 [6/100]
Effective Python Learning Memorandum Day 12 [12/100]
Effective Python Learning Memorandum Day 9 [9/100]
Effective Python Learning Memorandum Day 8 [8/100]
Effective Python Learning Memorandum Day 14 [14/100]
Effective Python Learning Memorandum Day 1 [1/100]
Effective Python Learning Memorandum Day 13 [13/100]
Effective Python Learning Memorandum Day 3 [3/100]
Effective Python Learning Memorandum Day 5 [5/100]
Effective Python Learning Memorandum Day 4 [4/100]
Effective Python Learning Memorandum Day 7 [7/100]
Effective Python Learning Memorandum Day 2 [2/100]
Python learning day 4
Python memorandum
Python Memorandum 2
Python memorandum
python learning
python memorandum
python memorandum
Python day 1
Python memorandum
python memorandum
Python memorandum
Python basics memorandum
[Python] Learning Note 1
Python learning notes
Python memorandum (algorithm)
python learning output
Deep Learning Memorandum
Python learning site
Python Deep Learning
Python learning (supplement)
Deep learning × Python
Python memorandum [links]
python learning notes
Python study day 1
Machine learning starting with Python Personal memorandum Part2
Machine learning starting with Python Personal memorandum Part1
Python memorandum numbering variables
Python class (Python learning memo ⑦)
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Python module (Python learning memo ④)
Reinforcement learning 1 Python installation
Learning Python with ChemTHEATER 05-1
Python: Deep Learning Practices
python memorandum (sequential update)
Python: Unsupervised Learning: Basics
Learning record 4 (8th day)
Learning record 9 (13th day)
[1day1lang AdventCalender] day4 Python
Learning record 3 (7th day)
Learning record 5 (9th day)
Learning record 6 (10th day)
Python memorandum (personal bookmark)
Programming learning record day 2
Learning record 8 (12th day)
Learning record 1 (4th day)
Learning record 7 (11th day)
Private Python learning procedure