【发布时间】:2016-07-19 07:45:34
【问题描述】:
在 get 方法中,如果我的默认函数有异常,那么无论字典中是否存在键,控件总是转到默认函数。这是演示相同的代码块。
编译器细节如下:
Python 2.7.6(默认,2015 年 4 月 8 日,11:18:18)
[GCC 4.7.2] 在 linux2 上
abc = 1
bcd = 2
class a(object):
def test1(self):
alphaNumMap = {
'a':abc,
'b':bcd
}
num = alphaNumMap.get("a", self.add())
print num
def add(self):
print "add called "
raise Exception
if __name__ == "__main__":
A = a()
A.test1()
以上代码的输出如下:
add called
Traceback (most recent call last):
File "test2.py", line 19, in <module>
A.test1()
File "test2.py", line 10, in test1
ipXpath = alphaNumMap.get("a",self.add())
File "test2.py", line 15, in add
raise Exception
Exception
虽然预期的输出是 1 ,因为 'a' 存在于 dict alphaNumMap 中
【问题讨论】:
标签: python exception dictionary data-structures default