ValueError:[]不在列表中

时间:2018-09-04 12:55:42

标签: python-3.x

我的代码有一些问题。

  

这是我的代码

class LocalSessionStorage(Borg):
SessionList = List[Session]
__sessions: SessionList = []

def add_session(self, session: Session):
    self.__sessions.append(session)

def get_session_list(self):
    return self.__sessions

def get_session_by_connection(self, ws_connection: WSConnectionType):
    return [i for i in self.__sessions if i.connection.ws_connection == ws_connection]

def delete_session(self, session: Session):
    index = self.__sessions.index(session)
    self.__sessions.pop(index)
  

这   追溯(最近一次通话):

  File "/usr/lib/python3.6/site-packages/tornado/web.py", line 1499, in _stack_context_handle_exception

    raise_exc_info((type, value, traceback))

  File "<string>", line 4, in raise_exc_info

  File "/usr/lib/python3.6/site-packages/tornado/stack_context.py", line 315, in wrapped

    ret = fn(*args, **kwargs)

  File "/usr/lib/python3.6/site-packages/tornado/websocket.py", line 440, in on_connection_close

    self.on_close()

  File "/opt/rm-interface-platform-transmitter-server/controller/api/v1/ws/handlers/connector.py", line 65, in on_close

    LOCAL_SESSION_STORAGE.delete_session(session)

  File "/opt/rm-interface-platform-transmitter-server/controller/helpers/ws/router/connection_storage.py", line 58, in delete_session

    index = self.__sessions.index(session)

ValueError: [] is not in list

我知道,问题出在这种方法中:

def delete_session(self, session: Session):
    index = self.__sessions.index(session)
    self.__sessions.pop(index)

但是我不明白,我该如何解决。我希望有人能帮助我。

1 个答案:

答案 0 :(得分:0)

首先,让我们看一下代码,提高质量。

  1. 我总是会{@ {1}}而不是使用getter和setter。

示例:

property

用法:

@property
def sessions(self):
    return self.__sessions
  1. 您收到的错误实际上是在告诉您YourClass().sessions 不在列表中,您如何调用这些函数?父类的目的是什么-通过查看问题中的最少数据量;我会说您正在将无效数据传递给函数。