[python] super (), inheritance, __init__, etc.

Introduction

This is just a memo of the operation check. Referenced here

environment

python 3.6.9

Think in multiple classes

--Fly variable in Bird class --Swim variable in Fish class --The Penguin class is an inheritance of the Bird and Fish classes.

Failure example (1) Class variables are not inherited

class Bird:
    def __init__(self):
        self.fly = 'i can flying'
        print(self.__class__.__name__ + 'Create class')
        
class Fish:
    def __init__(self):
        self.swim = 'i can swimming'
        print(self.__class__.__name__ + 'Create class')
        
class Penguin(Class1, Class2):
    def __init__(self):
        print(self.__class__.__name__ + 'Create class')       
        
        
penguin = Penguin()
penguin.fly


#Execution result
AttributeError: 'Penguin' object has no attribute 'fly'

Failure example (2) Succession failure

Fish class variable is overwritten by Bird class's __init__ function


class Bird():
    def __init__(self):
        self.fly = 'i can flying'
        print(self.__class__.__name__)
        
class Fish():
    def __init__(self):
        self.swim = 'i can swimming'
        print(self.__class__.__name__)
        
class Penguin(Bird, Fish):
    def __init__(self):
        super().__init__()
        print(self.__class__.__name__)
        
        
penguin = Penguin()
penguin.fly
penguin.swim


#Execution result
AttributeError: 'Penguin' object has no attribute 'swim'

Success story

It can be inherited without being overwritten by explicitly inheriting the __init__'function with the super () function in the __init __` function of each class.

class Bird(object):
    def __init__(self):
        super(Bird, self).__init__()
        self.fly = 'i can flying'
        print('In the Bird class')
        
class Fish(object):
    def __init__(self):
        super(Fish, self).__init__()
        self.swim = 'i can swimming'
        print('In the Fish class')
        
class Penguin(Bird, Fish):
    def __init__(self):
        super(Penguin,self).__init__()
        print('In the Fish class')
        
        
penguin = Penguin()
print(penguin.fly)
print(penguin.swim)

Think with Pokemon

Basic configuration

--The Pokemon class has life and power class variables --Pichu class is inheritance of Pokemon class --Pikachu class is an inheritance of Pichu class --Richu class is inheritance of Pikachu class

Initialize with default arguments

--Set initial values for Pichu, Pikachu, Raichu

class Pokemon():
    def __init__(self, life=0, power=0):
        self.life = life 
        self.power = power 
        print('In Pokemon class')
        print(self.__class__.__name__ + 'class')
        print('life: {} power: {}'.format( self.life, self.power))
        print('\n↓\n')
        
class Pichu(Pokemon):
    def __init__(self, life=50, power=100):
        super().__init__(life, power)
        print('In Pichu class')
        print(self.__class__.__name__ + 'class')
        print('life: {} power: {}'.format( self.life, self.power))
        print('\n↓\n')
    def kamituku(self):
        print('Biting')
        
class Pikachu(Pichu):
    def __init__(self, life=150, power=200):
        super().__init__(life, power)
        print('In Pikachu class')
        print(self.__class__.__name__ + 'class')
        print('life: {} power: {}'.format( self.life, self.power))
        print('\n↓\n')
    def denkishokku(self):
        print('Electric shock')
        
class Raichu(Pikachu):
    def __init__(self, life=200, power=250):
        super().__init__(life, power)
        print('In Raichu class')
        print(self.__class__.__name__ + 'class')
        print('life: {} power: {}'.format( self.life, self.power))
        print('\n')
    def kaminari(self):
        print('Thunder')
    def set(self, life, power):
        self.life = life
        self.power = power
        
print('---------------------------')
print('◯ pokemon instance ◯')
print('---------------------------')
pokemon = Pokemon()

print('---------------------------')
print('◯ pichu instance ◯')
print('---------------------------')
pichu = Pichu()

print('---------------------------')
print('◯ pikachu instance ◯')
print('---------------------------')
pikachu = Pikachu()

print('---------------------------')
print('◯ raichu instance ◯')
print('---------------------------')
raichu = Raichu()
raichu.kamituku()
raichu.denkishokku()
raichu.kaminari()

Execution result


---------------------------
◯ pokemon instance ◯
---------------------------
In Pokemon class
Pokemon class
life: 0 power: 0

↓

---------------------------
◯ pichu instance ◯
---------------------------
In Pokemon class
Pichu class
life: 50 power: 100

↓

In Pichu class
Pichu class
life: 50 power: 100

↓

---------------------------
◯ pikachu instance ◯
---------------------------
In Pokemon class
Pikachu class
life: 150 power: 200

↓

In Pichu class
Pikachu class
life: 150 power: 200

↓

In Pikachu class
Pikachu class
life: 150 power: 200

↓

---------------------------
◯ raichu instance ◯
---------------------------
In Pokemon class
Raichu class
life: 200 power: 250

↓

In Pichu class
Raichu class
life: 200 power: 250

↓

In Pikachu class
Raichu class
life: 200 power: 250

↓

In Raichu class
Raichu class
life: 200 power: 250


Biting
Electric shock
Thunder

Think with humans

Basic configuration

--Human has yaruki member variables --Tarou is inheriting Human --The initial value of yaruki of Tarou is 100 --Set tarou's yaruki to 50

Part ①

--Human has initialization variables --Tarou inheritance of initialization variables

class Human:
    def __init__(self, yaruki):
         self.yaruki = yaruki
            
class Tarou(Human):
    def __init__(self,yaruki):
        super().__init__(yaruki)
    def set(self, yaruki):
        self.yaruki = yaruki
        
tarou = Tarou(100)
tarou.set(50) 
print(tarou.yaruki)

#Execution result
50

Part 2

--Neither Human nor Tarou have initialization variables --Tarou has an initial value of 100 for yaruki

class Human:
    def __init__(self):
         self.yaruki
            
class Tarou(Human):
    def __init__(self):
        self.yaruki = 100
    def set(self, yaruki):
        self.yaruki = yaruki
        
        
tarou = Tarou()
tarou.set(50) 
tarou.yaruki

#Execution result
50

Recommended Posts

[python] super (), inheritance, __init__, etc.
[Python] Class inheritance (super)
Python #inheritance (inheritance)
About python inheritance
Convenient Python methods, etc.
[Python] Super useful debugging
[Python] Class inheritance, override
python memorandum super basic
python super beginner tries scraping
Twitter posts on Python 3 etc.
Python #function 2 for super beginners
Python for super beginners Python #functions 1
Python #list for super beginners
Class inheritance and super functions
[Python of Hikari-] Chapter 09-03 Class (inheritance)
Samples of Python getters, setters, etc.
Python #index for super beginners, slices
Python #len function for super beginners
[Python] Reasons for overriding using super ()
Python #Hello World for super beginners
Python for super beginners Python # dictionary type 2 for super beginners