python字典get函数陷阱
2011-10-31 15:35
323 查看
python字典get函数有一个很微妙的陷阱,代码如下:
>>> t={1:2}
>>> s={3:4}
>>> t.get(1,s[1])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 1
>>> t.get(1,s.get(1,0))
2
python字典的get函数在执行时,会同时检查执行get函数的字典,同时还会去获取默认值,最后再决定返回值。
>>> t={1:2}
>>> s={3:4}
>>> t.get(1,s[1])
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 1
>>> t.get(1,s.get(1,0))
2
python字典的get函数在执行时,会同时检查执行get函数的字典,同时还会去获取默认值,最后再决定返回值。
相关文章推荐
- python字典结构get()方法的陷阱
- python中字典处理函数get和setdefault的区别
- Python的字典get方法:从字典中获取一个值
- python getaddrinfo 函数
- 【python】Python的字典get方法:从字典中获取一个值
- Python 从字典取值—dict.get()
- 初学Python的学习笔记2----dist字典,set集合,声明函数,函数参数
- 【Python笔记】no.2字典和函数及练习
- Python函数参数默认值的陷阱和原理深究"
- Python的魔法函数之 - __len__,__getitem__,__setitem__,__delitem__
- Python字典get()方法的实际应用
- Python 一些特别函数 __getitem__ __getattr__
- python中函数陷阱
- 在Python中用get()方法获取字典键值的教程
- python中的字典操作及字典函数
- Python 字典(Dictionary) get()方法
- python 字典(dict)get方法应用
- Python语言——Pandas包中的get_dummy()函数用法
- Python函数中接受元组和字典
- python中常用的字典内建函数