python運行出現TypeError: super() takes at least 1 argument (0 given)錯誤


在寫繼承子類的時候出現了TypeError: super() takes at least 1 argument (0 given)的error;

源代碼(python3中完美可運行):

class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI() #界面繪制交給InitUi方法
        
        

 

原因是super().__init__()函數在python3中支持,是正確的,但是放到python2中會出現問題;

如果在python2想要繼承父類的構造方法,則需要給super參數中傳入參數:super(Example,self).__init__();

python2中需這樣寫:

class Example(QWidget):
    
    def __init__(self):
        super(Example,self).__init__()
        
        self.initUI() #界面繪制交給InitUi方法
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM