未定义回溯类

2024-09-24 06:30:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个正在进行的项目,它引用了其他类中的几个类

代码:

try:
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
except:
    input('Please install PyQt5 before using the GUI system. (press enter to close)')
    exit()

class app(QApplication):
    def __init__(self):
        super(app, self).__init__([])
        print('running')
        andGate = gate()
        andGate.config('and', 2)

    def initUI(self):
        newAndBtn = QPushButton(self)
        newOrBtn = QPushButton(self)
        newXorBtn = QPushButton(self)

    def newAndGate(self):
        pass

class gate():
    gateType = 'none'
    _in = []
    _out = pinOut()

    def _init__(self):
        _in.append(pinIn())
        _in.append(pinIn())
        pass

    def config(self, gateType, inputTargets):
        #just some safety checks
        if gateType not in supportedGates:
            return False

        self.gateType = gateType
        if type(inputTargets) is not list:
            return False

        for i in inputTargets:
            if type(i) is not pinOut:
                return False

        self.gateType = gateType
        for i in range(len(self._in)):
            self._in[i].point(inputTargets[i])

    def update(self):
        if _in[0].fetch() and _in[1].fetch():
            _out.set(True)
        else:
            _out.set(False)
        print("update for {}".format(self))

class pinIn():
    value = True
    reference = None

    def __init__(self):
        pass

    def point(target):
        if type(target) is not connOut:
            return False
        reference = target

    def fetch():
        self.value = reference.value
        return self.value

class pinOut():
    value = False

    def __init__(self):
        pass

    def set(self, newValue):
        if type(newValue) is not bool:
            return False
        self.value = newValue

创建app()类的实例时,会得到一个回溯:

Traceback (most recent call last):
  File "C:\Users\ccronk22\Documents\Python_Scripts\Logic\run.py", line 1, in <module>
    import main
  File "C:\Users\ccronk22\Documents\Python_Scripts\Logic\main.py", line 8, in <module>
    class app(QApplication):
  File "C:\Users\ccronk22\Documents\Python_Scripts\Logic\main.py", line 23, in app
    class gate():
  File "C:\Users\ccronk22\Documents\Python_Scripts\Logic\main.py", line 26, in gate
    output = pinOut()
NameError: name 'pinOut' is not defined

我曾尝试将pinIn和pinout类移到gate类中,然后将它们全部移到app类中,但这些都不起作用。在此之前,我一直将_-in声明为包含两个pinIn实例的列表,但得到了相同的错误

为什么gate类看不到pinIn和pinout类


Tags: inselffalseappreturnifinitvalue
1条回答
网友
1楼 · 发布于 2024-09-24 06:30:09

pinOut在引用它之后定义为

class gate():
    gateType = 'none'
    _in = []
    _out = pinOut()    # referenced here
    ...

class pinOut():        # defined here
    ...

pinOut的定义移到gate的定义之前,或者更好地将其设置在__init__

class gate():
    gateType = 'none'

    def __init__(self):
        self._in = [pinIn(), pinIn()]
        self._out = pinOut()

您应该更喜欢在__init__中初始化东西的原因是,这样可以避免在多个实例之间共享状态,这很可能是您想要避免的

相关问题 更多 >