Python 正在读取我的 类 中的一个作为变量并给出参考错误
Python is reading one of my classes in as a variable and giving a reference error
我有一个 if/elif 语句将对象附加到堆栈中。该代码对于语句的前 3 部分工作正常,但对于 else 部分给出错误。我尝试了一些不同的东西,但它们最终会给出更多错误。
在此先感谢您的帮助!
class nfa:
intial, accept = None, None
def __init__(self, initial, accept):
self.intial, self.accept = initial, accept
def compile(postfix):
nfaStack = []
for c in postfix:
# join the initial and accept states together to create a loop for your characters
if c == '*':
nfa = nfaStack.pop()
initial, accept = state(), state()
initial.edge1, nfa.accept.edge1 = nfa.intial
initial.edge2, nfa.accept.edge2 = accept, accept
nfaStack.append(nfa(initial, accept))
# merge the two automata by linking 1's accept to 2's initial states
elif c == '.':
nfa2, nfa1 = nfaStack.pop(), nfaStack.pop()
nfa1.accept.edge1 = nfa2.intial
nfaStack.append(nfa1.intial, nfa2.accept)
# create new initial and accept states and use them to link nfa1 and nfa2
elif c == '|':
nfa2, nfa1 = nfaStack.pop(), nfaStack.pop()
initial, accept = state(), state()
initial.edge1, initial.edge2 = nfa1.intial, nfa2.intial
# both old accept states now point to our new accept state
nfa1.accept.edge1, nfa2.accept.edge1 = accept, accept
nfaStack.append(nfa(initial, accept))
# creates new states and edges; labels each edge with what the current non-special character is
else:
initial, accept = state(), state()
initial.label = c
initial.edge1 = accept
# create instance of class nfa()
nfaStack.append(nfa(initial, accept))
# should only ever have one nfa in the stack
return nfaStack.pop()
您收到此错误是因为您不小心将 nfa
重新定义为此处的变量:
# join the initial and accept states together to create a loop for your characters
if c == '*':
nfa = nfaStack.pop() # `nfa` is now a variable!
最简单的解决办法就是将class大写为NFA
,这样一目了然什么是变量什么是class名字。这意味着像这样更改行:
# This:
nfaStack.append(nfa(initial, accept))
# Becomes:
nfaStack.append(NFA(initial, accept))
我有一个 if/elif 语句将对象附加到堆栈中。该代码对于语句的前 3 部分工作正常,但对于 else 部分给出错误。我尝试了一些不同的东西,但它们最终会给出更多错误。 在此先感谢您的帮助!
class nfa:
intial, accept = None, None
def __init__(self, initial, accept):
self.intial, self.accept = initial, accept
def compile(postfix):
nfaStack = []
for c in postfix:
# join the initial and accept states together to create a loop for your characters
if c == '*':
nfa = nfaStack.pop()
initial, accept = state(), state()
initial.edge1, nfa.accept.edge1 = nfa.intial
initial.edge2, nfa.accept.edge2 = accept, accept
nfaStack.append(nfa(initial, accept))
# merge the two automata by linking 1's accept to 2's initial states
elif c == '.':
nfa2, nfa1 = nfaStack.pop(), nfaStack.pop()
nfa1.accept.edge1 = nfa2.intial
nfaStack.append(nfa1.intial, nfa2.accept)
# create new initial and accept states and use them to link nfa1 and nfa2
elif c == '|':
nfa2, nfa1 = nfaStack.pop(), nfaStack.pop()
initial, accept = state(), state()
initial.edge1, initial.edge2 = nfa1.intial, nfa2.intial
# both old accept states now point to our new accept state
nfa1.accept.edge1, nfa2.accept.edge1 = accept, accept
nfaStack.append(nfa(initial, accept))
# creates new states and edges; labels each edge with what the current non-special character is
else:
initial, accept = state(), state()
initial.label = c
initial.edge1 = accept
# create instance of class nfa()
nfaStack.append(nfa(initial, accept))
# should only ever have one nfa in the stack
return nfaStack.pop()
您收到此错误是因为您不小心将 nfa
重新定义为此处的变量:
# join the initial and accept states together to create a loop for your characters
if c == '*':
nfa = nfaStack.pop() # `nfa` is now a variable!
最简单的解决办法就是将class大写为NFA
,这样一目了然什么是变量什么是class名字。这意味着像这样更改行:
# This:
nfaStack.append(nfa(initial, accept))
# Becomes:
nfaStack.append(NFA(initial, accept))