[Python] in python2 and 3, a brief description of super method for class inheritance

Keywords: Python

Here is an example. The same code is written with python2 and python3. Please pay attention to the red and bold parts in the two programs:

python2 class inheritance uses the super method:

 1 #-*-  coding:utf-8 -*-
 2 '''
 3 Created on 2018 August 27th 2013
 4 
 5 @author: anyd
 6 '''
 7 import random as r
 8 
 9 class Fish(object):
10     def __init__(self):
11         self.x = r.randint(0, 10)
12         self.y = r.randint(0, 10)
13             
14     def move(self):
15         #This is mainly to demonstrate the inheritance mechanism of classes, which does not consider the problem of checking scene boundary and moving direction
16         #Let's say all the fish are going west all the way
17         self.x -= 1
18         print "My position is:", self.x, self.y
19 
20 class Goldfish(Fish):
21     pass
22 
23 class Carp(Fish):
24     pass
25 
26 class Salmon(Fish):
27     pass
28 
29 #The top few are all food. Food doesn't need to have personality, so it can be inherited directly Fish All the properties and methods of the class
30 #The shark class is defined below. This is food, except for inheritance Fish Class, and add a method to eat
31 
32 class Shark(Fish):
33     def __init__(self):
34         super(Shark,self).__init__()       
35         self.hungry = True
36 
37     def eat(self):
38         if self.hungry:
39             print "The dream of eating is to eat every day^_^"
40             self.hungry = False
41         else:
42             print "Too much to eat!"
43             
44 aa = Shark()
45 aa.move()

The output is as follows:

My position is: 8 2

  

The class inheritance of python3 uses the super method:

 1 #-*-  coding:utf-8 -*-
 2 '''
 3 Created on 2018 August 27th 2013
 4 
 5 @author: anyd
 6 '''
 7 import random as r
 8 
 9 class Fish(object):
10     def __init__(self):
11         self.x = r.randint(0, 10)
12         self.y = r.randint(0, 10)
13             
14     def move(self):
15         #This is mainly to demonstrate the inheritance mechanism of classes, which does not consider the problem of checking scene boundary and moving direction
16         #Let's say all the fish are going west all the way
17         self.x -= 1
18         print ("My position is:", self.x, self.y)
19 
20 class Goldfish(Fish):
21     pass
22 
23 class Carp(Fish):
24     pass
25 
26 class Salmon(Fish):
27     pass
28 
29 #The top few are all food. Food doesn't need to have personality, so it can be inherited directly Fish All the properties and methods of the class
30 #The shark class is defined below. This is food, except for inheritance Fish Class, and add a method to eat
31 
32 class Shark(Fish):
33     def __init__(self):
34         super().__init__()       
35         self.hungry = True
36 
37     def eat(self):
38         if self.hungry:
39             print ("The dream of eating is to eat every day^_^")
40             self.hungry = False
41         else:
42             print ("Too much to eat!")
43             
44 aa = Shark()
45 aa.move()

The output is as follows:

My position is: 7 4

  

Please refer to the official guidance documents for the specific use of super method. There are detailed use examples in it. However, I don't think this super method is easy for people to see. I prefer to use unbound method to write, so no matter it is python2 or python3, there is no problem. As follows:

1 class Shark(Fish):
2     def __init__(self):
3         Fish.__init__(self)     
4         self.hungry = True

Posted by k4pil on Mon, 06 Jan 2020 21:14:43 -0800