python超类初始化参数

时间:2017-05-30 09:20:33

标签: python initialization arguments superclass

我遇到了如何为超类初始化编写参数的问题。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="style.css"> <title>Index</title> </head> <body> <header> This is header </header> <main> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde sunt, eaque optio id nostrum ullam voluptatum incidunt modi autem nulla nihil, voluptatibus iusto consectetur. Est quas libero illum dolore dicta?</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde sunt, eaque optio id nostrum ullam voluptatum incidunt modi autem nulla nihil, voluptatibus iusto consectetur. Est quas libero illum dolore dicta?</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde sunt, eaque optio id nostrum ullam voluptatum incidunt modi autem nulla nihil, voluptatibus iusto consectetur. Est quas libero illum dolore dicta?</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde sunt, eaque optio id nostrum ullam voluptatum incidunt modi autem nulla nihil, voluptatibus iusto consectetur. Est quas libero illum dolore dicta?</p> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Unde sunt, eaque optio id nostrum ullam voluptatum incidunt modi autem nulla nihil, voluptatibus iusto consectetur. Est quas libero illum dolore dicta?</p> </main> <footer> This is footer </footer> </body> </html>需要继承其他两个类。我在class App1初始化了基类中的所有参数,但错误说我有太多args。我想知道为什么?基本上,我把基类中的所有参数都放在了超级init中。这3个类被编写为多个窗口,并且class App1命令将该类逐个跳转。因此,我将Button称为main()

感谢您的帮助!

myApp = Welcome(root, csv_name_ses, csv_name_sub)

错误消息:

class question(object): #first window
     def __init__(self, algorithmIndex, initX, mid_loss_list = None, mid_gain_list = None):
          self.initX = initX
          self.algorithmIndex = algorithmIndex
          self.mid_gain_list = question.mid_gain_list
          self.mid_loss_list = question.mid_loss_list
          ...

class Welcome(object): #second window
      def __init__(self, master, csv_name_ses, csv_name_sub):
          self.master = master
          Welcome.csv_name_sub = str(self.entrySub.get())
          Welcome.csv_name_ses = str(self.entrySes.get())
          ...

class App1(Welcome, question): #third, last one appears
      def __init__(self, master, csv_name_ses, csv_name_sub, algorithmIndex, initX, mid_loss_list, mid_gain_list):
          super(App1, self).__init__(master, csv_name_ses, csv_name_sub, algorithmIndex, initX, mid_loss_list, mid_gain_list)
          ...    

def main():

   root = Tk()
   myApp = Welcome(root, csv_name_ses, csv_name_sub)
   root.mainloop()

2 个答案:

答案 0 :(得分:1)

您的Welcomequestion课程不是合作超类。如果要使用super(..),则需要重写其__init__方法以接受任意数量的参数,并且需要使用尚未使用的所有参数再次调用super(..).__init__。 / p>

但是对于你的情况,显式调用超类初始化器可能更容易:

class Appl(Welcome, question):
    def __init__(self, master, csv_name_ses, csv_name_sub, algorithmIndex, initX, mid_loss_list, mid_gain_list):
        Welcome.__init__(self, master, csv_name_ses, csv_name_sub)
        question.__init__(self, algorithmIndex, initX, mid_loss_list, mid_gain_list)

答案 1 :(得分:1)

你应该这样打电话:

var a = '123', b = '111x';
var intA = parseInt(a), intB = parseInt(b);
if(a == intA){
    console.log(a is a valid integer string); // this gets printed
}else{
    console.log('a is not a valid integer string');
}

if(b == intB){
    console.log(b is a valid integer string); 
}else{
    console.log('b is not a valid integer string');// this gets printed
}