Python dict.get()引发了KeyError [英] Python dict.get() raises KeyError

查看:315
本文介绍了Python dict.get()引发了KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里迷路了,Python 2.7,我有一个字典 mt ,我使用 get()方法,它通过文档说:

I am getting lost here, Python 2.7, I have a dictionary mt, and I use the get() method, which by documentation says:


get(key [,default])如果键位于
字典中,则返回键值,否则返回默认值。如果没有给出默认值,它默认为
无,因此该方法从不引发 KeyError

但我还是得到

 File "/home/ubuntu/subscription-workers/commands/dr/rebilling.py", line 48, in rebill
    if mt.get('is_rebill', 0) == 1:
 KeyError: 'is_rebill'

任何想法为什么?

mt 是一个普通的 dict ,有时没有密钥。

The mt is a normal dict, that sometimes does not have the key.

推荐答案

所以我把问题搞定了。在这段代码被放置到位之前,有一个这样的一个

So I nailed the problem down. Before this code was put in place there was this one

File "/home/ubuntu/subscription-workers/commands/dr/rebilling.py", line 48, in rebill
    if mt['is_rebill'] == 1:
KeyError: 'is_rebill'

问题是旧版本中有.pyc文件,但堆栈跟踪正在加载实际的代码。运行后

The problem was that there were .pyc files from the older version, but the stack trace was loading the actual code. After running

find . -name "*.pyc" -exec rm -rf {} \;

并重新加载应用程序的一切都很好,没有问题。

and reloading the app everything was fine and without problems.

这篇关于Python dict.get()引发了KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆