我正在尝试用 python 创建一个状态机(我知道有一个模块,我试图不使用它们),它给我一个重新分配的错误,我想知道我是否需要担心它:
Expression of type "ResetState | sysCheck" cannot be assigned to member "currState" of class "Globals"
Member "set" is unknown
Type "ResetState | sysCheck" cannot be assigned to type "ResetState"
"sysCheck" is incompatible with "ResetState"PylancereportAttributeAccessIssue
所以我的代码在 2 个文件中
全局.py
from StateMachine import States
class Globals:
states = States()
currState = states.ResetStateObject
状态机.py
from Globals import Globals
class States:
def __init__(self):
self.ResetStateObject = self.ResetState()
self.sysCheckObject = self.sysCheck()
class ResetState:
def transistion_function(self):
return
def run_function(self):
return Globals.states.sysCheckObject
class sysCheck:
def transistion_function(self):
return
def run_function(self):
return Globals.states.sysCheckObject
class StateMachine:
def __init__(self):
self.new_state = Globals.states.ResetStateObject
def tick(self): ## run this in a loop to run the statemachine ||
if Globals.currState == self.new_state:
self.new_state = Globals.currState.run_function()
else:
Globals.currState = self.new_state
Globals.currState.transition_function()
问题是我是否可以忽略该错误
我想给类的对象分配相同的函数名,并且在main函数中方便读取
我无法测试它,因为它应该在 micropython 上运行,并且很多支持代码尚未准备好。不过我认为应该没问题,但我无法弄清楚自己是否需要以与现在不同的方式看待课程
错误就上线了
Globals.currState = self.new_state
通过将全局类移动到与状态机相同的文件中修复了错误,有人称之为循环导入