简体   繁体   中英

Why am I getting the error: __init__() missing 1 required positional argument?

Fedora 34, Python 3.9.5

I wrote code with a class that inherits from the other two (one of which inherits the QWidget functionality). As a result, this simple class doesn't initialized.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget
 
class M_A(type(QWidget), type):pass
class A(QWidget, metaclass=M_A):
    def __init__(self):
        QWidget.__init__(self)
         
class B:
    def __init__(self, controller):
        self.controller = controller
         
         
class M_C(type(A), type(B)):pass
class C(A, B, metaclass=M_C):
    def __init__(self, controller):
        A.__init__(self)
        B.__init__(self, controller)
         
         
app = QtWidgets.QApplication([])
C(controller=19)


Traceback (most recent call last):
  File "/home/ivan/eclipse-workspace/123/Exp.py", line 22, in <module>
    C(controller=19)
  File "/home/ivan/eclipse-workspace/123/Exp.py", line 17, in __init__
    A.__init__(self)
  File "/home/ivan/eclipse-workspace/123/Exp.py", line 7, in __init__
    QWidget.__init__(self)
TypeError: __init__() missing 1 required positional argument: 'controller'

Where is the mistake?

The docs in the official example of implementing mixings points out

This follows a similar pattern to our Person implementation, but notice that we have provided the .

(emphasis mine)

Applied in your case controller must have a default value, for example 0:

class B:
    def __init__(self, controller):
        self.controller = controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM