Python class inheritance bug

     When writing class inheritance recently, I found that the inheritance of python needs to change the parameters of the base class in order to call the constructor of the parent class. After inheriting the parent class, especially for multiple inheritance, I found that the parameter transfer was unsuccessful. Finally, I modified the parent class. The order of the member variables is successful (colleagues who have other ideas are welcome to leave their own thoughts, I can only solve the problem, and have not found the reason). The question and all the code have been uploaded on github.

The requirements are as follows

Item is the parent class, the member variables are name and aomunt, and the member functions are buy, sell and get operations respectively;

First, a subclass of NewBook ​​needs to inherit Item, only the amont variable is used, and the name is a fixed string. And it can only inherit its member variables and member methods, and NewBook ​​cannot overwrite Item methods and variables. In the following code, the author uses two methods in the subclass NewBook ​​to call the parent class construction method, but they all fail. And reported the same error!

 

#父类
class Item(object):
    def __init__(self,name,amount):
        self.name = name
        self.amount = amount
        
    def buy(self, number):
        self.amount += number

    def sell(self, number):
        self.amount -= number
     
        
    def getName(self):
        return self.name
        
    def getAmount(self):
        return self.amount


#子类
class NewBook(Item):
    def __init__(self,amount):
        #Item.__init__(self,name = "new book",amount)  #第一种方法调用父类构造函数
        super(NewBook, self).__init__(name = "new book"amount)  #第2种方法调用父类构造函数
        
    def buy(self):
        Item.buy(self, number=10)
    
    def sell(self):
        Item.sell(self, number=1)

Results of the first construction method: 

 

The second method reports an error result:

 Because the name I want to pass is a fixed parameter and aomunt is a user-defined parameter, it can only be passed like this. Then why can't it be passed to the constructor of the parent class, or the constructor of the parent class did not recognize the passed parameters? After I modified the order of passing parameters of the parent class, this problem was solved (the problem mentioned in the title)

class Item(object):
    def __init__(self,amount,name):   #改变部分
        self.name = name
        self.amount = amount
       
        
    def buy(self, number):
        self.amount += number
        

    def sell(self, number):
        self.amount -= number
        
        
    def getName(self):
        return self.name
        
    def getAmount(self):
        return self.amount
        

class NewBook(Item):
    def __init__(self,amount):
       
        super(NewBook, self).__init__(amount,name = "new book") #改变部分
        
    def buy(self):
        Item.buy(self, number=10)
    
    def sell(self):
        Item.sell(self, number=1)

After passing the default parameters, the amount is placed in the front

 

If you need to further understand the reason and analysis, you can directly visit the warehouse to view. The problem background and all the code have been uploaded on github

Guess you like

Origin blog.csdn.net/qq_39463175/article/details/107734171