has-key-shelve方法的不稳定行为

2024-05-15 12:48:12 发布

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

我一直在使用python的shelve库(不是在多处理环境中),并且在使用has_key()方法时观察到不稳定的行为。当我知道密钥存在于书架中时,它有时返回True,有时返回False。造成这种行为的通常原因是什么?在

import shelve

some_key = 'not relevant'
s = shelve.open('cache.db')
if s.has_key(some_key):
   var = s[some_key]
   print 'found key', some_key

s.close()

注意:Python2.7,Ubuntu14.04在虚拟机虚拟机中,运行在i7x86-64位


Tags: 方法keyimportfalsetrue环境密钥not