class Pacco:
STATES = ('Il pacco e` stato ordinato ma non ancora spedito',
'Il pacco e` stato spedito ma non ancora ricevuto',
'Il pacco e` stato ricevuto')
indice = 0
def __init__(self):
self.state = Pacco.STATES[0]
self._succ = Pacco.STATES[1]
self._prec = None
def next(self):
print(self.state)
@property
def state(self):
Pacco.indice += 1
self.state = Pacco.STATES[Pacco.indice]
self._succ = Pacco.STATES[Pacco.indice+1]
self._prec = Pacco.STATES[Pacco.indice-1]
在indice = 0
处出现错误Unexpected indent
,在行Pacco.indice += 1
处出现错误
Unindent does not match any outer indentation level
然后在接下来的3行中给出错误信息
Unresolved reference 'self'...
有人可以告诉我为什么吗?
答案 0 :(得分:1)
您混用了制表符和空格。将所有制表符转换为空格(per PEP 8),语法工作正常。
注意:Stack Overflow格式化程序将制表符转换为四个空格,因此您在帖子中看不到它,但是如果选中source,则可以。
答案 1 :(得分:0)
首先,将所有预定义变量保留在__init__
函数中。其次,self.state
变量必须为self._state
,因为存在一个具有相同名称的函数。第三,请参阅@wjandrea's answer以获取有关制表符和空格格式的建议。代码应如下所示:
class Pacco:
def __init__(self):
self.STATES = ('Il pacco e` stato ordinato ma non ancora spedito',
'Il pacco e` stato spedito ma non ancora ricevuto',
'Il pacco e` stato ricevuto')
self.indice = 0
self._state = self.STATES[0]
self._succ = self.STATES[1]
self._prec = None
def next(self):
print(self.state)
@property
def state(self):
self.indice += 1
self._state = self.STATES[self.indice]
self._succ = self.STATES[self.indice+1]
self._prec = self.STATES[self.indice-1]
答案 2 :(得分:0)
您可以使用autopep8
pip install autopep8
现在在终端中输入autopep8 <yourfile.py>
并输入
看到代码现在将以正确的方式格式化